Creating And Accessing Dictionary Value in Python Programming

python

Dictionary in Python programming is a data type. they are an unordered collection of data values, and usually hold key-value pairs like Map. We will learn when the dictionary data type is the right one to use and how to access and manage it. Dictionaries are mutable and dynamic and can grow and shrink when needed. The Dictionaries elements are usually accessed via keys. To get Python Development you can Download it from this page here

How to create and Add Values to Python Dictionary

The Syntax used in Python program to create a dictionary is by placing the elements in curly braces ({}) and those elements are separated by a comma. In the Pair of Data being held by Dictionary one usually is the Key and the other pair element is the value of the Key(Key: value). The Key value is always immutable and unique in a dictionary. The Keys are case sensitive, if a key has the same value but different cases, the key will always be treated differently but for the value, any data type can be used and can even be repeated.

Empty Dictionary

Diction = {}
print("Empty Dictionary Created: ")
print(Diction )

Dictionary creation with the key being integers

Diction = {1: 'One', 2: 'Two', 3: 'Three'}
print("\nDictionary creation with the key being integers: ")
print(Diction )

Dictionary creation with the key being Mixed

Diction = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Diction )

Dictionary creation Using the Built-in Dict() Method

Diction = dict({1: ‘One’, 2: ‘Two’, 3:’Three’})
print(“\nDictionary creation using dict(): “)
print(Diction )

Dictionary creation Using the Built-in Dict() Method each item being a pair

Diction = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary creation with each item as a pair: ")
print(Diction )

Adding elements to a Dictionary one by one

Diction = {1: 'One', 2: 'Two', 3: 'Three'}

item to the dictionary adding using a new Key and assigning a value

diction[1]="Four"
print(Diction )
Adding elements to a Dictionary using update() Method
Diction = {1: 'One', 2: 'Two', 3: 'Three'}

item to the dictionary adding using Update(), When the item is not in the dictionary the item gets added

diction.update({1:"Four"})
print(Diction )

When Removing item from a dictionary pop(),del(),Clear() and popitem() Methods are usually used.

Pop() is a method used for any item we have specified the key name

Diction = {1: ‘One’, 2: ‘Two’, 3: ‘Three’}
Diction.pop(1)
print(Diction )

popitem() usually removes the last item that was added.

Diction = {1: 'One', 2: 'Two', 3: 'Three'}
Diction.popitem()
print(Diction )

del() does have two options remove any item we have specified the key name and also delete the dictionary completely

Delete item with the specified key

Diction = {1: 'One', 2: 'Two', 3: 'Three'}
del Diction[1]
print(Diction )

delete the dictionary completely

Diction = {1: ‘One’, 2: ‘Two’, 3: ‘Three’}
del Diction
print("\nDictionary creation with the key being integers: ")
print(Diction )

How to Access elements in a Python Dictionary

When accessing the dictionary items, we refer to its key name also we can use the get Method

creating a dictionary

Diction = {1: 'One', 2: 'Two', 3: 'Three'}

Accessing elements in the dictionary using key

print(Diction[1]) #returns One

Accessing using the get() Method

print(Diction.get(2) # Returns Two

Python Programming Dictionary Data type

Total
9
Shares
Leave a Reply

Your email address will not be published.

Previous Post

Common PostgreSQL GUI Clients to use in Ubuntu

Next Post

Verizon 4G LTE Internet and Home Phone Configuration

Related Posts