Unpack Dictionary In Python
Last Updated :
04 May, 2025
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)
OutputBob
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)
OutputBob
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)
OutputBob
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.
Related Articles
Similar Reads
Unpacking Dictionary Keys into Tuple - Python The task is to unpack the keys of a dictionary into a tuple. This involves extracting the keys of the dictionary and storing them in a tuple, which is an immutable sequence.For example, given the dictionary d = {'Gfg': 1, 'is': 2, 'best': 3}, the goal is to convert it into a tuple containing the key
2 min read
Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop
2 min read
Python - Unique Values of Key in Dictionary We are given a list of dictionaries and our task is to extract all the unique values associated with a given key. For example, consider: data = [ {"name": "Aryan", "age": 25}, {"name": "Harsh", "age": 30}, {"name": "Kunal", "age": 22}, {"name": "Aryan", "age": 27}]key = "name"Then, the unique values
4 min read
Python - Iterate over Tuples in Dictionary In this article, we will discuss how to Iterate over Tuples in Dictionary in Python. Method 1: Using index We can get the particular tuples by using an index: Syntax: dictionary_name[index] To iterate the entire tuple values in a particular index for i in range(0, len(dictionary_name[index])): print
2 min read
Create Dictionary Of Tuples - Python The task of creating a dictionary of tuples in Python involves mapping each key to a tuple of values, enabling structured data storage and quick lookups. For example, given a list of names like ["Bobby", "Ojaswi"] and their corresponding favorite foods as tuples [("chapathi", "roti"), ("Paraota", "I
3 min read