Python Dictionary
Python Dictionary is a collection. It can contain multiple elements.
Python Dictionary is un-ordered. The elements in the dictionary are not stored in a sequence. For example, if you add an item to a dictionary, it can be inserted at any index.
Python Dictionary is changeable. You can update an item using its key, delete an item, or add an item. The original dictionary gets updated. Simply put, Python Dictionary is mutable.
Python Dictionary is indexed. You can access a specific key:value pair using key as index.
Create a Dictionary
To create a python dictionary, assign a variable with comma separated key:value pairs enclosed in curly braces.
In the following example, we create a dictionary with some initial key:value pairs.
#initialize tuple aDict = { 'tallest building':'Burj Khalifa', 'longest river':'The Nile', 'biggest ocean':'The Pacific Ocean' }
In the above example, tallest building
, longest river
and biggest ocean
are keys while Burj Khalifa
, The Nile
and The Pacific Ocean
are their corresponding values.
Access Specific Key:Value Pair in Dictionary
To access a specific key:value pair in a dictionary, use key as index on the dictionary.
Python Program
#initialize tuple aDict = { 'tallest building':'Burj Khalifa', 'longest river':'The Nile', 'biggest ocean':'The Pacific Ocean' } print(aDict['longest river'])Try Online
Output
The Nile
Iterate through key:value pairs of Dictionary
To iterate through all key:value pairs of a Python Dictionary, you can use for loop as shown below.
Python Program
#initialize tuple aDict = { 'tallest building':'Burj Khalifa', 'longest river':'The Nile', 'biggest ocean':'The Pacific Ocean' } for key in aDict: print(key,':',aDict[key])Try Online
Output
tallest building : Burj Khalifa longest river : The Nile biggest ocean : The Pacific Ocean
Add a new Key:Value Pair to the Dictionary
To add a new key value pair to the dictionary, just assign the value to the dictionary using key as index.
Python Program
#initialize tuple aDict = { 'tallest building':'Burj Khalifa', 'longest river':'The Nile', 'biggest ocean':'The Pacific Ocean' } aDict['biggest forest'] = 'The Amazon' for key in aDict: print(key, ':', aDict[key])Try Online
Output
tallest building : Burj Khalifa longest river : The Nile biggest ocean : The Pacific Ocean biggest forest : The Amazon
Dictionary Operations
You can perform many other operations on Dictionary. You can refer these following tutorials for different operations available on a Python Dictionary.
- Get Keys of Python Dictionary as a ListPython Tutorial to get all the keys in the given dictionary as a list.
- Get Values of Python Dictionary as a ListPython Tutorial to get all the values in the given dictionary as a list.
- Get Length of Python DictionaryPython Tutorial to get the length of given dictionary.
Conclusion
In this Python Tutorial, we learned how to initialize a Dictionary in Python, how to iterate through the key:value pairs of the dictionary, how to add a new key:value pair to the dictionary and many other operations that could be performed on a Python Dictionary.