0

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)

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.