0

I have a function f(a,b,c) waiting for 3 int. I have a list t = list(1,2,3) that i want to input to f : f(t). But it doesn't work. What is the easiest way to do it.

EDIT : t = list('1','2','3')

Thank you

0

3 Answers 3

5

You need to unpack the values present in list t.

 f(*t)

Example:

>>> def f(a,b,c):
        print(a,b,c)

>>> t = [1,2,3]
>>> f(*t)
1 2 3

OR

>>> def f(a,b,c):
        print(a,b,c)


>>> t = ['1','2','3']
>>> f(*map(int,t))
1 2 3
Sign up to request clarification or add additional context in comments.

3 Comments

What if a,b,c are strings and i want to change them in the same time to integers ?
Will the code break if the list has only 2 items (and c doesn't have a default value)? Just curious.
yep, because the defined function needs exactly three parameters. You could set the default value of c like def f(a,b,c=0):
0

You will have to pass the values as follows:

f(t[0], t[1], t[2])

Comments

0

You can call the function like this:

f(t[0],t[1],t[2])

Cheers!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.