The Unpacking Operator * in Lists and Dictionaries
Published:
• Last updated:
• By Jeferson Peter
Python
Imagine you have a list of values or multiple dictionaries and want to combine them easily.
Python’s unpacking operators*
and**
give you a clean way to expand sequences and mappings.
Using * in function calls
def add(a, b, c):
return a + b + c
values = [1, 2, 3]
print(add(*values))
# 6
Using * in list creation
numbers = [1, 2, 3]
more = [*numbers, 4, 5]
print(more)
# [1, 2, 3, 4, 5]
Using ** in dictionaries
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3}
merged = {**dict1, **dict2}
print(merged)
# {'a': 1, 'b': 2, 'c': 3}
Conclusion
The unpacking operators make it simple to expand, merge, and pass values dynamically.
They improve readability and reduce boilerplate code.