Junior — Middle
Is it possible to use a list as a parameter when calling a function?
sobes.tech AI
Answer from AI
Yes, in Python you can use a list as a parameter when calling a function. The list is passed as an object, and the function can work with it like any other parameter.
Example:
def print_elements(elements):
for element in elements:
print(element)
my_list = [1, 2, 3, 4]
print_elements(my_list)
You can also use the unpacking operator * to pass the list elements as separate positional arguments:
def add(a, b, c):
return a + b + c
numbers = [1, 2, 3]
result = add(*numbers) # equivalent to add(1, 2, 3)
print(result) # 6
Thus, lists are conveniently used as function parameters.