Skip to content

Dictionary Operations

Dictionary Operations

We are going to learn dictionary view objects, dictionary comprehension, and how to merge dictionaries. Looping through dictionaries is also covered. Copying dictionaries is also covered. Nested dictionaries are also covered. In this tutorial, we will learn how to perform operations on Python dictionaries.

Dictionary View Objects

Dictionary view objects are objects that provide a dynamic view of dictionary keys and values. Dictionary view objects are returned by the methods keys()keys(), values()values(), and items()items(). Dictionary view objects are dynamic, meaning that they change when the dictionary changes. Dictionary view objects are iterable, meaning that you can loop through them. Dictionary view objects are also set-like, meaning that they do not allow duplicate elements. Dictionary view objects are not dictionaries, but they behave like them.

Dictionary Keys View Object

The dictionary keys view object is returned by the keys()keys() method. The dictionary keys view object is a set-like object that provides a dynamic view of the dictionary keys. The dictionary keys view object is iterable, meaning that you can loop through it. The dictionary keys view object is not a dictionary, but it behaves like one. The dictionary keys view object is dynamic, meaning that it changes when the dictionary changes. The dictionary keys view object is set-like, meaning that it does not allow duplicate elements. The dictionary keys view object is not a set, but it behaves like one.

dict_keys.py
data = {'a': 1, 'b': 2, 'c': 3}
keys = data.keys()
print(keys)
dict_keys.py
data = {'a': 1, 'b': 2, 'c': 3}
keys = data.keys()
print(keys)

Output:

command
C:\Users\username>python dict_keys.py
dict_keys(['a', 'b', 'c'])
command
C:\Users\username>python dict_keys.py
dict_keys(['a', 'b', 'c'])

In this example, we declare a dictionary and assign it to the variable datadata. We then call the keys()keys() method on the dictionary and assign the result to the variable keyskeys. We then print the variable keyskeys. The output shows that the variable keyskeys is a dictionary keys view object.

Dictionary Values View Object

The dictionary values view object is returned by the values()values() method. The dictionary values view object is a set-like object that provides a dynamic view of the dictionary values. The dictionary values view object is iterable, meaning that you can loop through it. The dictionary values view object is not a dictionary, but it behaves like one. The dictionary values view object is dynamic, meaning that it changes when the dictionary changes. The dictionary values view object is set-like, meaning that it does not allow duplicate elements. The dictionary values view object is not a set, but it behaves like one.

dict_values.py
data = {'a': 1, 'b': 2, 'c': 3}
values = data.values()
print(values)
dict_values.py
data = {'a': 1, 'b': 2, 'c': 3}
values = data.values()
print(values)

Output:

command
C:\Users\username>python dict_values.py
dict_values([1, 2, 3])
command
C:\Users\username>python dict_values.py
dict_values([1, 2, 3])

In this example, we declare a dictionary and assign it to the variable datadata. We then call the values()values() method on the dictionary and assign the result to the variable valuesvalues. We then print the variable valuesvalues. The output shows that the variable valuesvalues is a dictionary values view object.

Dictionary Items View Object

The dictionary items view object is returned by the items()items() method. The dictionary items view object is a set-like object that provides a dynamic view of the dictionary items. The dictionary items view object is iterable, meaning that you can loop through it. The dictionary items view object is not a dictionary, but it behaves like one. The dictionary items view object is dynamic, meaning that it changes when the dictionary changes. The dictionary items view object is set-like, meaning that it does not allow duplicate elements. The dictionary items view object is not a set, but it behaves like one.

dict_items.py
data = {'a': 1, 'b': 2, 'c': 3}
items = data.items()
print(items)
dict_items.py
data = {'a': 1, 'b': 2, 'c': 3}
items = data.items()
print(items)

Output:

command
C:\Users\username>python dict_items.py
dict_items([('a', 1), ('b', 2), ('c', 3)])
command
C:\Users\username>python dict_items.py
dict_items([('a', 1), ('b', 2), ('c', 3)])

In this example, we declare a dictionary and assign it to the variable datadata. We then call the items()items() method on the dictionary and assign the result to the variable itemsitems. We then print the variable itemsitems. The output shows that the variable itemsitems is a dictionary items view object.

Dictionary Comprehension

Dictionary comprehension is a way to create dictionaries using an iterable. Dictionary comprehension is similar to list comprehension. Dictionary comprehension is a way to create dictionaries using an iterable. Dictionary comprehension is similar to list comprehension.

dict_comprehension.py
data = {x: x * x for x in range(1, 6)}
print(data)
dict_comprehension.py
data = {x: x * x for x in range(1, 6)}
print(data)

Output:

command
C:\Users\username>python dict_comprehension.py
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
command
C:\Users\username>python dict_comprehension.py
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

In this example, we declare a dictionary using dictionary comprehension and assign it to the variable datadata. We then print the variable datadata. The output shows that the variable datadata is a dictionary.

Merging Dictionaries

Merging dictionaries is a way to combine two or more dictionaries into one dictionary. Merging dictionaries is a way to combine two or more dictionaries into one dictionary.

dict_merge.py
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = {'d': 4, 'e': 5, 'f': 6}
data3 = {**data1, **data2}
print(data3)
dict_merge.py
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = {'d': 4, 'e': 5, 'f': 6}
data3 = {**data1, **data2}
print(data3)

Output:

command
C:\Users\username>python dict_merge.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
command
C:\Users\username>python dict_merge.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this example, we declare two dictionaries and assign them to the variables data1data1 and data2data2. We then declare a third dictionary and assign it to the variable data3data3. We then merge the two dictionaries into the third dictionary using the **** operator. We then print the variable data3data3. The output shows that the variable data3data3 is a dictionary.

Merging Dictionaries Using update() Method

Merging dictionaries using the update()update() method is a way to combine two or more dictionaries into one dictionary. Merging dictionaries using the update()update() method is a way to combine two or more dictionaries into one dictionary.

dict_update.py
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = {'d': 4, 'e': 5, 'f': 6}
data3 = data1.copy()
data3.update(data2)
print(data3)
dict_update.py
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = {'d': 4, 'e': 5, 'f': 6}
data3 = data1.copy()
data3.update(data2)
print(data3)

Output:

command
C:\Users\username>python dict_update.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
command
C:\Users\username>python dict_update.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this example, we declare two dictionaries and assign them to the variables data1data1 and data2data2. We then declare a third dictionary and assign it to the variable data3data3. We then merge the two dictionaries into the third dictionary using the update()update() method. We then print the variable data3data3. The output shows that the variable data3data3 is a dictionary.

Looping Through Dictionaries

Looping through dictionaries is a way to iterate through the keys, values, or items of a dictionary. Looping through dictionaries is a way to iterate through the keys, values, or items of a dictionary.

Looping Through Dictionary Keys

Looping through dictionary keys is a way to iterate through the keys of a dictionary. Looping through dictionary keys is a way to iterate through the keys of a dictionary.

dict_keys_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for key in data.keys():
    print(key)
dict_keys_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for key in data.keys():
    print(key)

Output:

command
C:\Users\username>python dict_keys_loop.py
a
b
c
command
C:\Users\username>python dict_keys_loop.py
a
b
c

In this example, we declare a dictionary and assign it to the variable datadata. We then loop through the dictionary keys using a forfor loop. We then print the dictionary key. The output shows that the dictionary keys are printed.

Looping Through Dictionary Values

Looping through dictionary values is a way to iterate through the values of a dictionary. Looping through dictionary values is a way to iterate through the values of a dictionary.

dict_values_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for value in data.values():
    print(value)
dict_values_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for value in data.values():
    print(value)

Output:

command
C:\Users\username>python dict_values_loop.py
1
2
3
command
C:\Users\username>python dict_values_loop.py
1
2
3

In this example, we declare a dictionary and assign it to the variable datadata. We then loop through the dictionary values using a forfor loop. We then print the dictionary value. The output shows that the dictionary values are printed.

Looping Through Dictionary Items

Looping through dictionary items is a way to iterate through the items of a dictionary. Looping through dictionary items is a way to iterate through the items of a dictionary.

dict_items_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for key, value in data.items():
    print(key, value)
dict_items_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for key, value in data.items():
    print(key, value)

Output:

command
C:\Users\username>python dict_items_loop.py
a 1
b 2
c 3
command
C:\Users\username>python dict_items_loop.py
a 1
b 2
c 3

In this example, we declare a dictionary and assign it to the variable datadata. We then loop through the dictionary items using a forfor loop. We then print the dictionary key and value. The output shows that the dictionary items are printed.

Lopping using in Keyword

Lopping using the inin keyword is a way to iterate through the keys of a dictionary. Lopping using the inin keyword is a way to iterate through the keys of a dictionary.

dict_in_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for key in data:
    print(key)
dict_in_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for key in data:
    print(key)

Output:

command
C:\Users\username>python dict_in_loop.py
a
b
c
command
C:\Users\username>python dict_in_loop.py
a
b
c

In this example, we declare a dictionary and assign it to the variable datadata. We then loop through the dictionary keys using a forfor loop. We then print the dictionary key. The output shows that the dictionary keys are printed.

Looping using enumerate() Function

Looping using the enumerate()enumerate() function is a way to iterate through the keys of a dictionary. Looping using the enumerate()enumerate() function is a way to iterate through the keys of a dictionary.

dict_enumerate_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for index, key in enumerate(data):
    print(index, key)
dict_enumerate_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
for index, key in enumerate(data):
    print(index, key)

Output:

command
C:\Users\username>python dict_enumerate_loop.py
0 a
1 b
2 c
command
C:\Users\username>python dict_enumerate_loop.py
0 a
1 b
2 c

In this example, we declare a dictionary and assign it to the variable datadata. We then loop through the dictionary keys using a forfor loop. We then print the dictionary key. The output shows that the dictionary keys are printed.

Lopping using range() Function

Lopping using the range()range() function is a way to iterate through the keys of a dictionary. Lopping using the range()range() function is a way to iterate through the keys of a dictionary.

dict_range_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
length = len(data)
for i in range(length):
    print(list(data.keys())[i] + " => " + str(list(data.values())[i]))
dict_range_loop.py
data = {'a': 1, 'b': 2, 'c': 3}
length = len(data)
for i in range(length):
    print(list(data.keys())[i] + " => " + str(list(data.values())[i]))

Output:

command
C:\Users\username>python dict_range_loop.py
a => 1
b => 2
c => 3
command
C:\Users\username>python dict_range_loop.py
a => 1
b => 2
c => 3

In this example, we declare a dictionary and assign it to the variable datadata. We then declare a variable and assign it to the length of the dictionary. We then loop through the dictionary keys using a forfor loop. We then print the dictionary key and value. The output shows that the dictionary keys and values are printed.

Copying Dictionaries

Copying dictionaries is a way to create a copy of a dictionary. Copying dictionaries is a way to create a copy of a dictionary.

Direct Copy

Direct copy is a way to create a copy of a dictionary. Direct copy is a way to create a copy of a dictionary.

dict_direct_copy.py
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = data1
print(data2)
dict_direct_copy.py
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = data1
print(data2)

Output:

command
C:\Users\username>python dict_direct_copy.py
{'a': 1, 'b': 2, 'c': 3}
command
C:\Users\username>python dict_direct_copy.py
{'a': 1, 'b': 2, 'c': 3}

In this example, we declare a dictionary and assign it to the variable data1data1. We then declare a second dictionary and assign it to the variable data2data2. We then print the variable data2data2. The output shows that the variable data2data2 is a dictionary.

copy() Method

Shallow copy is a way to create a copy of a dictionary. Shallow copy is a way to create a copy of a dictionary.

dict_shallow_copy.py
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = data1.copy()
data2['d'] = 4
print(data1)
print(data2)
dict_shallow_copy.py
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = data1.copy()
data2['d'] = 4
print(data1)
print(data2)

Output:

command
C:\Users\username>python dict_shallow_copy.py
{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
command
C:\Users\username>python dict_shallow_copy.py
{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

In this example, we declare a dictionary and assign it to the variable data1data1. We then declare a second dictionary and assign it to the variable data2data2. We then add an item to the second dictionary. We then print the variable data1data1 and data2data2. The output shows that the variable data1data1 and data2data2 are dictionaries. The output also shows that the variable data1data1 and data2data2 are not the same dictionary.

deepcopy() Method

Deep copy is a way to create a copy of a dictionary. Deep copy is a way to create a copy of a dictionary.

dict_deep_copy.py
import copy
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = copy.deepcopy(data1)
data2['d'] = 4
print(data1)
print(data2)
dict_deep_copy.py
import copy
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = copy.deepcopy(data1)
data2['d'] = 4
print(data1)
print(data2)

Output:

command
C:\Users\username>python dict_deep_copy.py
{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
command
C:\Users\username>python dict_deep_copy.py
{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

In this example, we import the copycopy module. We then declare a dictionary and assign it to the variable data1data1. We then declare a second dictionary and assign it to the variable data2data2. We then add an item to the second dictionary. We then print the variable data1data1 and data2data2. The output shows that the variable data1data1 and data2data2 are dictionaries. The output also shows that the variable data1data1 and data2data2 are not the same dictionary.

Conclusion

In this tutorial, we learned how to perform operations on Python dictionaries. We learned dictionary view objects, dictionary comprehension, and how to merge dictionaries. Looping through dictionaries is also covered. Copying dictionaries is also covered. We are now able to perform operations on Python dictionaries. For more information on Python dictionaries, visit the official Python dictionaries documentation. For more tutorials on Python, visit the Python Central Hub.

Was this page helpful?

Let us know how we did