Python Dictionary

Filed Under: Python

Today we are going to learn Python Dictionary. Previously we learned about Python Set.

Python Dictionary

A python dictionary is basically a sequence of key-value pair. This means, for each key, there should be a value. All the keys are unique.

We can initialize a dictionary closed by curly braces. Key and values are separated by a semicolon and the values are separated by the comma. Dictionaries are easy to use. The following code will help you understand Python Dictionary.


my_dictionary = {} #init empty dictionary

#init dictionary with some key-value pair
another = {
            #key    : value,
            'man'   : 'Bob',
            'woman' : 'Alice',
            'other' : 'Trudy'
        }

#print initial dictionaries
print(my_dictionary)
print(another)

#insert value
my_dictionary['day']='Thursday'
another['day']='Thursday'
my_dictionary['color']='Blue'
another['color']='Blue'

#print updated dictionaries
print('Updated Dictionaries:')
print(my_dictionary)
print(another)

#update values
my_dictionary['day']='Friday'
another['day']='Friday'
my_dictionary['color']='Black'
another['color']='Black'

#print updated dictionaries
print('After Update:')
print(my_dictionary)
print(another)

#printing a single element
print(my_dictionary['day'])
print(another['color'])

The output of the following code will be


{}
{'woman': 'Alice', 'other': 'Trudy', 'man': 'Bob'}
Updated Dictionaries:
{'color': 'Blue', 'day': 'Thursday'}
{'color': 'Blue', 'woman': 'Alice', 'other': 'Trudy', 'day': 'Thursday', 'man': 'Bob'}
After Update:
{'color': 'Black', 'day': 'Friday'}
{'color': 'Black', 'woman': 'Alice', 'other': 'Trudy', 'day': 'Friday', 'man': 'Bob'}
Friday
Black
>>> 

Accessing Python Dictionary

We can access dictionary elements through keys. If the keys are not known, then we can use for loop to iterate over the dictionary elements.


dictionary = {
    'name'  : 'Alex',
    'age'   : 23,
    'sex'   : 'male'
    }

#method1
print('Method1')

#fetch all the keys of that dictionary
key_list = dictionary.keys() #store the key list in key_list

#print to see the keys
print('list of keys')
print(key_list)

#pick key from the key_list
for key in key_list:
    #print the specific value for the key
    print('key = '+key+' value = '+str(dictionary[key]))

#method2
print('\nMethod2')

#pick key from directly from the dictionary
for key in dictionary:
    #print the specific value for the key
    print('key = '+key+' value = '+str(dictionary[key]))

It will generate the following output


Method1
list of keys
['age', 'name', 'sex']
key = age value = 23
key = name value = Alex
key = sex value = male

Method2
key = age value = 23
key = name value = Alex
key = sex value = male
>>> 

Deleting Elements from Python Dictionary

Deletion of elements in python dictionary is quite easy. You can just use del keyword. It will delete a single element from Python Dictionary. But if you want to delete all elements from the dictionary. You can use clear() function. Deletion of elements from Python Dictionary is shown in the following code:


dictionary = {
    'name'  : 'Alex',
    'age'   : 23,
    'sex'   : 'male'
    }

#print initial dictionary
print(dictionary)

#delete a single element
del dictionary['name']
print('After deleting name')
print(dictionary)


'''
you cannot the element which is not in the dictionary. so the below statement
will raise an error

del dictionary['name']
'''


#delete all elements from the list
dictionary.clear()
print(dictionary) #this will show an empty dictionary

#delete the entire variable
del dictionary
print(dictionary) #this will produce error

The output of the code be like

Python Dictionary

If you want to know more about the functions of Python Dictionary you can see the official reference. You can find it here.

That’s all for Python Dictionary Tutorial.

You can checkout complete python script and more Python examples from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages