Computer >> Computer tutorials >  >> Programming >> Python

How does * operator work on list in Python?


The star(*) operator unpacks the sequence/collection into positional arguments. So if you have a list and want to pass the items of that list as arguments for each position as they are there in the list, instead of indexing each element individually, you could just use the * operator. 

example

def multiply(a, b):
  return a * b
values = [1, 2]
print(multiply(*values))

This will unpack the list so that it actually executes as −

print(multiply(1, 2))

Output

This will give the output −

2