In this Python Dictionary tutorial, we will learn what a dictionary is, how to create a dictionary, how to access the elements of a dictionary, etc.

Python Dictionary

Python Dictionary is a collection. It can contain multiple elements. Each element is a key-value pair.

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.

</>
Copy
#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

</>
Copy
#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 in Dictionary

To iterate through all key:value pairs of a Python Dictionary, you can use Python For Loop as shown below.

Python Program

</>
Copy
#initialize tuple
aDict = {
	'tallest building':'Burj Khalifa',
	'longest river':'The Nile',
	'biggest ocean':'The Pacific Ocean'
}

for key in aDict:
	print(key,':',aDict[key])

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

</>
Copy
#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])

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.

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.