Open In App

Unpack Dictionary In Python

Last Updated : 04 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Unpacking a dictionary in Python means extracting its values and assigning them to variables, often in a more structured or readable way. For example, given a dictionary d = {"name": "Bob", "age": 22, "major": "Computer Science"}, you can unpack its values into separate variables like a = "Bob", b = 22, and c = "Computer Science". Let’s explore the most efficient methods to achieve this.

Using multiple assignment with .values()

This is the most straightforward way to unpack dictionary values into separate variables. It works perfectly when the number of variables matches the number of dictionary values and you know the order of the items.

Python
d = {"name": "Bob", "age": 22, "major": "Computer Science"}
a, b, c = d.values()

print(a)  
print(b)  
print(c)  

Output
Bob
22
Computer Science

Explanation: d.values() method returns values in order ["Bob", 22, "Computer Science"] which are unpacked into a, b and c in one simple step.

Using * (Star) Unpacking

This method is powerful when you need to extract both keys and values separately. By using zip(*d.items()), you effectively transpose the dictionary into two distinct tuples one containing all the keys and the other all the values. It’s especially useful for structured processing, like formatting data into tables or exporting it.

Python
d = {"name": "Bob", "age": 22, "major": "Computer Science"}
k, v = zip(*d.items())

print(list(k))  
print(list(v))

Output
['name', 'age', 'major']
['Bob', 22, 'Computer Science']

Explanation: zip(*d.items()) expression splits the dictionary into two tuples one for keys k and one for values v. Converting them to lists gives ["name", "age", "major"] for keys and ["Bob", 22, "Computer Science"] for values.

Using dict.values()

In this method, you first convert the dictionary values into a list and then access each element by its index. This gives you precise control over which value you want to retrieve and assign.

Python
d = {"name": "Bob", "age": 22, "major": "Computer Science"}
v = list(d.values())

a = v[0]
b = v[1]
c = v[2]

print(a) 
print(b)  
print(c)  

Output
Bob
22
Computer Science

Explanation: d.values() method extracts the dictionary values into a list v and each value is accessed by its index (v[0], v[1], v[2]) and assigned to a, b and c.

Using dictionary comprehensions

This technique involves recreating the dictionary using a comprehension and then unpacking the values from that newly constructed dictionary. It’s useful when you need to filter or transform data while extracting it.

Python
d = {"name": "Bob", "age": 22, "major": "Computer Science"}
a, b, c = {key: value for key, value in d.items()}.values()

print(a) 
print(b)  
print(c) 

Output
Bob
22
Computer Science

Explanation: dictionary comprehension creates a new dictionary from d and .values() extracts the values, which are then unpacked into a, b and c.


Next Article

Similar Reads