In this tutorial, I’m going to show you how to use dictionaries in Python. How to build, view, add, delete elements from them and different built-in techniques.
We’ll be covering the following topics in this tutorial:
What are Dictionaries in Python
Dictionary in Python are like associative lists or a map. Now you can think of a dictionary as a list of key:value pairs. A dictionary is an unordered, changeable collection which does not allow duplicates. Let me show you how to define a dictionary.
D = {'Name': 'max', 'age': 14, 'year': 2020}
You can specify any variable name and define a dictionary, first of all, you use these curly brackets {}
, and inside these curly brackets, you provide a list of key-value pairs. Let’s give a list of key-value pairs.
Accessing Elements from Dictionary
I’m going to access the items inside this dictionary. Which is D, and you can see our dictionary is created. Now, as I said dictionary is a list of key-value pairs and all these values. Which you see here before the colon are called keys. Name is a key here, and age is a key, the year is key. Whatever you see after the colon are called values, so max is a value. 2020 is a value and 14 is a value, and you can access the values from a dictionary-based upon their keys. for example
#!/usr/bin/python D = {'Name': 'max', 'age': 14, 'year': 2020} print(D['Name']) Output: max
Above dictionary D. I can use the square bracket []
and the key name, for example, I want to get the name value. I can give the name key here. It’s going to return me the Associated value related to key name in the same way you can use other keys also. for example
#!/usr/bin/python D = {'Name': 'max', 'age': 14, 'year': 2020} print(D['age']) Output: 14
It’s going to give me 14. which is the value. Age is a key here, and 14 is the value.
Dictionary Items – Data Types
You can define any data type as a key. Let me explain a new dictionary here, and I’m going to give the curly brackets {}
and as I said you could define a string value as key. You can specify a number as a key. for example
E = {'Name': 'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5}
Now to access let’s say we want to access the value for (2,3)
key. which is tuple. it’s going to return me five. it’s going to return me the value which is associated with it.
#!/usr/bin/python E = {'Name': 'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5} print(E[(2,3)]) Output: 5
Now, what happens when a key is not there, and we try to access it. I’m going to access a hundred from this a dictionary. It’s going to give us an error that this key is not present in the dictionary.
#!/usr/bin/python E = {'Name': 'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5} print(E[100]) Output: KeyError: 100
Dictionary Length
You can also use the len()
method to find out the number of items in the dictionary, and you can see it says five items are there in the dictionary.
#!/usr/bin/python E = {'Name': 'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5} print(len(E)) Output: 5
Above len()
function will return you the number of key-value pairs stored in the dictionary.
You can also use, for example, I’m going to use my D dictionary. Now and you can also use a method called get()
and then give the key name here in the parenthesis. Let’s say I want to get that value associated with the name key. I can get it.
#!/usr/bin/python D = {'Name': 'max', 'age': 14, 'year': 2020} print(D.get('Name')) Output: max
it’s going to give me the value associated with the name key.
Updating, adding and deleting Dictionary elements
You can also add a new key to D dictionary. Now three key-value pairs and I can add one more key-value pair to add a key-value pair you need to write D and in the square bracket []
. It would be best if you gave the name of the new key. I’m going to write surname here. The name of the new key in the dictionary D and then you need to give the value associated with that key.
#!/usr/bin/python D = {'Name': 'max', 'age': 14, 'year': 2020} D['Surname'] = 'Thakur' print(D) Output: {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}
You can see that surname is added to your dictionary.
If you want to remove any key-value pair from a list, you can use the pop()
method and then the name of the key, which you want to remove. Let’s say we want to remove the surname, which we have added.
#!/usr/bin/python D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'} D.pop('Surname') print(D) Output: {'Name': 'max', 'age': 14, 'year': 2020}
You can update the values in a dictionary. In D dictionary I want to update the name for example. I can use the dictionary name and then the key. for example
#!/usr/bin/python D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'} D['Name'] = 'Dinesh' print(D) Output: {'year': 2020, 'age': 14, 'Surname': 'Thakur', 'Name': 'Dinesh'}
Python Dictionary clear()
You can also use a clear()
function. Let’s see what’s there in the D dictionary. These are the values inside the dictionary. I can use clear the values inside the dictionary.
#!/usr/bin/python D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'} D.clear() print(D) Output: {}
When I try to access D, it will give me the empty dictionary {}
.
Python Dictionary del()
You can delete the dictionary using the del()
function and the name of the dictionary.
#!/usr/bin/python D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'} del D print(D) Output: NameError: name 'D' is not defined
When I try to access this dictionary D. it’s going to say that D name is not defined.
Python Dictionary keys()
Now there is a function in a dictionary called keys()
. Which is used to list out all the keys of that dictionary. you can see
#!/usr/bin/python D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'} print(D.keys()) Output: dict_keys(['age', 'Name', 'Surname', 'year'])
It will list out all the keys of the particular dictionary.
Python Dictionary values()
There is also a function called values()
, which will list all the dictionary values if you want to list out all the key-value pairs.
#!/usr/bin/python D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'} print(D.values()) Output: dict_values(['max', 2020, 'Thakur', 14])
Python Dictionary items()
You can use the function called items. It will give you the key-value pair list.
#!/usr/bin/python D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'} print(D.items()) Output: dict_items([('year', 2020), ('Surname', 'Thakur'), ('Name', 'max'), ('age', 14)])