Python Dictionary

Dictionary is a special associative object type in Python in which data are saved as a collection of key and value pairs in the form of {key:value}. It allows data access based on a key rather than an index and stores objects based on their key. There is no order in which the pairs are stored. Dictionaries are mutable in nature, so elements can be added, altered, and deleted. A dictionary cannot use another dictionary object as a key, but an element's value can be any valid Python type, immutable or mutable. Also, the values of two or more keys can be the same, but the keys themselves need to be unique.

How to create a Dictionary in Python?

In Python, a Dictionary is created by entering the key:value pairs (separated by commas) inside the curly braces, and a colon operator is used to separate each of these key and value pairs. The word left of the colon is a key, and the word to the right is a value of an element.

Like this

			

my_dict = {"Mercury":1, "Venus":2, "Earth":3, "Mars":4}
print(my_dict)

				

Output:

{'Mercury': 1, 'Venus': 2, 'Earth': 3, 'Mars': 4}

How to access elements of a Dictionary?

You can access a value in a dictionary by using its key. Just put the key inside square brackets to access it. Like this

			

my_dict = {"Mercury":1, "Venus":2, "Earth":3, "Mars":4}
print(my_dict["Earth"])

				

Output:

3

The get() function is used to access an element inside the dictionary.

			

my_dict = {"Mercury":1, "Venus":2, "Earth":3, "Mars":4}
print(my_dict.get("Earth"))

				

Output:

3

How to modify elements in a Dictionary?

Adding new elements and updating the existing ones is very simple in Python DIctionaries. Specify the key in the square brackets "[ ]" and use the "=" operator to assign the value to the key. If the key already exists in the dictionary, its value gets updated with the new value, and if the specified key does not exist, then a new key is created inside the dictionary and the value given is assigned to it.

This is done as follows

			

my_dict = {"Mercury":1, "Venus":2, "Earth":3, "Mars":4}
my_dict["Mercury"] = 6 #this key already exists so its value will be updated
my_dict["Jupiter"] = 7 #this key does not exist so it will be added to the dictionary
print(my_dict)

				

Output:

{'Mercury': 6, 'Venus': 2, 'Earth': 3, 'Mars': 4, 'Jupiter': 7}

How to remove elements from a Dictionary?

There are multiple ways to delete elements from a dictionary. You can use the del operator to delete an element with a particular key.

			

my_dict = {"Mercury":1, "Venus":2, "Earth":3, "Mars":4}
del my_dict["Mercury"]
print(my_dict)

				

Output:

{'Venus': 2, 'Earth': 3, 'Mars': 4}

The pop() function removes the key from the dictionary and returns its value. If the key is not present, then it returns the default.

			

my_dict = {"Mercury":1, "Venus":2, "Earth":3, "Mars":4}
print(my_dict.pop("Mars"))
print(my_dict)

				

Output:

4 {'Mercury': 1, 'Venus': 2, 'Earth': 3}

The clear() function removes all elements of the dictionary.

			

my_dict = {"Mercury":1, "Venus":2, "Earth":3, "Mars":4}
my_dict.clear()
print(my_dict)

				

Output:

{}

Dictionary Comprehension

Like list comprehension, we also have dictionary comprehension in Python. You can quickly create dictionaries using simple expressions. Like this

			

my_dict = {x: x+2 for x in range(7)}
print(my_dict)

				

Output:

{0: 2, 1: 3, 2: 4, 3: 5, 4: 6, 5: 7, 6: 8}

Available Dictionary methods in Python

These are the Dictionary functions supported by Python.

Function Description
dict.fromkeys(keys, values) Returns a new dictionary with specified keys and values.
dict.get(key) Returns the value of the key.
dict.items() Returns an object containing the (key, value) pairs of dict.
dict.popitem() Pops a random element in the dictionary and returns it.
dict.pop(key) Pops and returns the element with the specified key.
dict.clear() Delete all the elements in dictionary.
dict.setdefault(key, value) Returns the value associated with the key. If the key is not present in the dictionary, insert the key and assign it the value specified.
dict.update(dict2) Updates dictionary with the key:value pairs inside dict2
dict.copy() Return a copy of the dictionary
dict.values() Return an object containing all the values inside the dictionary.