Python – Unpack Dictionary

Python – Unpack Dictionary

What is does unpacking a dictionary in Python mean?

The answer is, it means that the items in the dictionary are loaded into variables, or loaded as items into another dictionary.

In Python, you can unpack a dictionary using several methods like using dictionary unpacking operator, using a For loop, using dictionary methods like keys(), values(), or items(), etc. Let us go through them one by one.

1. Unpacking a dictionary using double asterisk in Python

The most common way to unpack a dictionary is to use the ** operator, also known as double asterisk or dictionary unpacking.

This operator allows you to pass the key-value pairs from a dictionary as keyword arguments to a function or to create a new dictionary.

In the following program, we are given a dictionary in my_dict with some initial key:value pairs. We have to unpack the key-value pairs of this dictionary into the keyword arguments of the function using dictionary unpacking operator **.

Python Program

my_dict = {
    'foo':12,
    'bar':14
}

def my_function(foo, bar):
    print(f"foo is {foo}.")
    print(f"bar is {bar}.")

my_function(**my_dict)
Run Code Copy

Output

foo is 12.
bar is 14.

Please note that the keys in the dictionary must match the parameter names in the function definition, how foo and bar did here.

Now, let us see how to unpack a dictionary in dictionary initializer {} to create a new dictionary.

In the following program, we create a new dictionary new_dict from dictionaries dict_1 and dict_2 using dictionary unpacking operator **.

Python Program

dict_1 = {
    'foo':12,
    'bar':14
}

dict_2 = {
    'moo':16,
    'koo':18
}

new_dict = {**dict_1, **dict_2}

print(new_dict)
Run Code Copy

Output

{'foo': 12, 'bar': 14, 'moo': 16, 'koo': 18}

2. Unpacking a dictionary using For loop in Python

We can iterate over the items in a dictionary using a For loop with the dictionary itself as iterator. In each iteration, we get access to the key, and through which we can access the value as well.

For example, in the following program, we are given a dictionary in my_dict with some initial key:value pairs. We shall use a For loop to unpack a dictionary item by item and print them to output.

Python Program

my_dict = {
    'foo':12,
    'bar':14
}

for key in my_dict:
    value = my_dict[key]
    print(f"key:{key}, value:{value}")
Run Code Copy

Output

key:foo, value:12
key:bar, value:14

3. Unpacking keys of a dictionary using keys() method in Python

We can unpack the keys of a dictionary alone, using dictionary keys() method.

For example, in the following program, we are given a dictionary in my_dict with some initial key:value pairs. We shall use dictionary keys() method to unpack only the keys of this dictionary into a list.

Python Program

my_dict = {
    'foo':12,
    'bar':14
}

keys = list(my_dict.keys())
print(keys)
Run Code Copy

Output

['foo', 'bar']

4. Unpacking values of a dictionary using values() method in Python

We can unpack the values of a dictionary alone, using dictionary values() method.

For example, in the following program, we are given a dictionary in my_dict with some initial key:value pairs. We shall use dictionary values() method to unpack only the values of this dictionary into a list.

Python Program

my_dict = {
    'foo':12,
    'bar':14
}

values = list(my_dict.values())
print(values)
Run Code Copy

Output

[12, 14]

5. Unpacking items of a dictionary into tuples of a list using items() method in Python

We can unpack the items of a dictionary into a list of tuples, where each tuple is a key value pair, using dictionary items() method.

For example, in the following program, we shall use dictionary items() method to unpack only the values of the dictionary my_dict into a list of tuples.

Python Program

my_dict = {
    'foo':12,
    'bar':14
}

values = list(my_dict.items())
print(values)
Run Code Copy

Output

[('foo', 12), ('bar', 14)]

Summary

In this tutorial, we learned about different ways of unpacking a dictionary like using dictionary unpacking operator, using a For loop, using methods of dictionary instances, etc., with help of well detailed Python programs.

Related Tutorials

Code copied to clipboard successfully 👍