Python Dictionary
Python Dictionary
Python Dictionary
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.
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.
#initialize tuple
aDict = {
'tallest building':'Burj Khalifa',
'longest river':'The Nile',
'biggest ocean':'The Pacific Ocean'
}
print(aDict['longest river'])
Output
The Nile
Iterate through key:value pairs of Python Dictionary
To iterate through all key:value pairs of a Python Dictionary, you can use for loop as shown below.
#initialize tuple
aDict = {
'tallest building':'Burj Khalifa',
'longest river':'The Nile',
'biggest ocean':'The Pacific Ocean'
}
Output
#initialize tuple
aDict = {
'tallest building':'Burj Khalifa',
'longest river':'The Nile',
'biggest ocean':'The Pacific Ocean'
}
Output
Dictionary Operations
You can perform many other operations on Dictionary. You can refer these following tutorials for different
operations available on a Python Dictionary.
⊩ Python Tutorial
⊩ Install Python
⊩ Python Variables
⊩ Python Comments
Control Statements
⊩ Python If
⊩ Python If Else
Python String
Functions
⊩ Python Functions
Python Collections
⊩ Python List
⊩ Python Dictionary
Advanced
⊩ Python Multithreading
Useful Resources