The * symbol in Python, when used in function calls or assignments, is known as the unpacking operator. It allows you to unpack elements from an iterable (like a list, tuple, or another iterable) and pass them as individual arguments or elements.
- Unpacking in Function Calls
def my_function(a, b, c):
print(a, b, c)
# Using unpacking with a list
my_list = [1, 2, 3]
my_function(*my_list) # Unpacks the list, equivalent to my_function(1, 2, 3)
This would output
1 2 3
- Unpacking in Assignments
You can also use * to capture multiple values from an iterable into a single variable. This is known as "extended unpacking."
a, *b, c = [1, 2, 3, 4, 5]
- a will be assigned the first value (1).
- c will be assigned the last value (5).
- b will be assigned the middle values as a list ([2,3,4]).