Dictionaries in Python

Dictionaries are ordered collection of items in which each item has a key : value pair. The key and value can be retrieved.

Dictionaries can be created by using curly brackets or by using the dict( ) constructor. The following program shows  curly braces and dict( ) constructor to create a dictionary.

InDict = {
    "Fruit": "Apple",
    "Vegetable": "Potato ",
    "Juice": "Mango"
}
print(InDict)

# Using dict() constructor

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
print(InDict)

Output:

{'Fruit': 'Apple', 'Vegetable': 'Potato ', 'Juice': 'Mango'}
{'Fruit': 'Apple', 'Vegetable': 'Potato ', 'Juice': 'Mango'}

The number of items in a dictionary can be found by using the len( ) function. Following program shows the use of len( ) function to determine the number of items.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
print(len(InDict))

Output:

3

Values in the dictionary can be accessed by using the key in a square bracket or by the use of  get( )  method.

The following program shows accessing of values in a dictionary.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
print(InDict["Vegetable"])
print(InDict.get("Vegetable"))

Output:

Potato 
Potato 

The keys( ) and values( ) method returns all the keys and values of the dictionary. The following program shows the use of keys( ) and values( ) method to get keys and values of a dictionary.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
print(InDict.keys())
print(InDict.values())

output:

dict_keys(['Fruit', 'Vegetable', 'Juice'])
dict_values(['Apple', 'Potato ', 'Mango'])

The items( ) method  returns all the key-value pairs of the dictionary. The following program shows the use of items( ) method.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
print(InDict.items())

Output:

dict_items([('Fruit', 'Apple'), ('Vegetable', 'Potato '), ('Juice', 'Mango')])

A value can be changed in a dictionary by using its key name.  The following program shows changing a value of dictionary by using its key name.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
InDict["Vegetable"] = "Cauliflower"
print(InDict)

Output:

{'Fruit': 'Apple', 'Vegetable': 'Cauliflower', 'Juice': 'Mango'}

Dictionary can be updated with a new key-value pair with the use of update( ) method. The following program shows the use of update( ) method.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
InDict.update({"Drinks": "Cold drinks"})
print(InDict)

Output:

{'Fruit': 'Apple', 'Vegetable': 'Potato ', 'Juice': 'Mango', 'Drinks': 'Cold drinks'}

A value can be added to the dictionary by using a new key and giving a value to it. The following program shows addition of a key and value. If the key is already present then it gets updated with the new value.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
InDict["Drinks"] = "Lemon water"
print(InDict)

Output:

{'Fruit': 'Apple', 'Vegetable': 'Potato ', 'Juice': 'Mango', 'Drinks': 'Lemon water'}

Dictionary values can be deleted by using the specific key and pop( ) method. Any random key and value pair can be deleted by using the popitem( ) method. Also any specified dictionary value can also be deleted by using the del keyword and key name. 

All the dictionary elements can be removed by using the clear( ) method. The following program shows the use of pop( ), popitem( ), clear( ) method and del keyword.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
InDict.pop("Vegetable")
print(InDict)

InDict.popitem()
print(InDict)

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
del InDict["Juice"]
print(InDict)

InDict.clear()
print(InDict)

Output:

{'Fruit': 'Apple', 'Juice': 'Mango'}
{'Fruit': 'Apple'}
{'Fruit': 'Apple', 'Vegetable': 'Potato '}
{}

A dictionary can be copied with the help of copy( ) method. The following program shows the use of copy( ) method.

InDict = dict({"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"})
NewDict = InDict.copy()
print(NewDict)

Output:

{'Fruit': 'Apple', 'Vegetable': 'Potato ', 'Juice': 'Mango'}

A dictionary can contain several other dictionaries which itself has key-value pairs. Such dictionaries are called nested dictionaries. The following program shows a nested dictionary.

InDict = {1:{"Fruit": "Apple", "Vegetable": "Potato ",  "Juice": "Mango"},
              2:{"Fruit": "Mango", "Vegetable": "Lady finger ",  "Juice": "Orange"}}
print(InDict)

Output:

{1: {'Fruit': 'Apple', 'Vegetable': 'Potato ', 'Juice': 'Mango'}, 2: {'Fruit': 'Mango', 'Vegetable': 'Lady finger ', 'Juice': 'Orange'}}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments