Reputation: 1993
How do you pass multiple values into a function?
I'm needing a function which allows the user to enter multiple values. When I call the function I want it to be able to handle an endless list of values like this:
search_columns_multi_val(column_name, 1, 2, 3, 4, ...
)
So, the list of numbers stored in the values parameter should come in as a list of values.
This is as far as I have got with the basic code:
def search_columns_multi_val(column, values):
searchlist = [] # the list storing the entered values
for value in searchlist:
print(value)
Upvotes: 0
Views: 262
Reputation: 1172
In Python we can use *args and **kwargs for passing infinite number of unknown params. Here is how you would use it in your code example:
def search_columns_multi_val(column, *args):
searchlist = [] # the list storing the entered values
for value in args:
searchlist.append(value)
print(searchlist)
Upvotes: 1
Reputation: 734
you looking for this?
def Sum(*args):
totalSum = 0
for number in args:
totalSum += number
print(totalSum)
# function call
Sum(5, 4, 3, 2, 1)
Upvotes: 0
Reputation: 15364
Here's how you can do it:
def f(column_name, *values):
for value in values:
print(value)
values
is a tuple (not a list). If you really need a list, all you need to do is values = list(values)
.
Here is how you can use it:
>>> f('name', 3, 5, 7, 9)
3
5
7
9
Upvotes: 0