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 Methods

The following are the individual tutorials for each of the dictionary class methods.

  • Dictionary MethodsPython Tutorial that covers examples for all the methods of dictionary class.
  • dict.clear()Python Tutorial for dictionary clear() method to remove all the items from the given dictionary.
  • dict.copy()Python Tutorial for dictionary copy() method to make a shallow copy of the given dictionary.
  • dict.get()Python Tutorial for dictionary get() method to get the value for a specified key from the given dictionary.
  • dict.items()Python Tutorial for dictionary items() method to get a view object for the items in the given dictionary.
  • dict.keys()Python Tutorial for dictionary keys() method to get a view object for the keys in the given dictionary.
  • dict.values()Python Tutorial for dictionary values() method to get a view object for the values in the given dictionary.
  • dict.fromkeys()Python Tutorial for dictionary fromkeys() method to create a dictionary with given collection of keys, and a default value.
  • dict.pop()Python Tutorial for dictionary pop() method to remove the entry with the specified key, from the given dictionary.
  • dict.popitem()Python Tutorial for dictionary popitem() method to remove an entry and return it, from the given dictionary.
  • dict.setdefault()Python Tutorial for dictionary setdefault() method to get the value for specified key from the dictionary, if the key is present. If the key is not present, then the method returns specified default value.
  • dict.update()Python Tutorial for dictionary update() method to update the values in the dictionary with the matching keys of an other dictionary.

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.