Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass each element of a list to a function that takes multiple arguments in Python?

For example, if I have

a=[['a','b','c'],[1,2,3],['d','e','f'],[4,5,6]]

How can I get each element of a to be an argument of say, zip without having to type

zip(a[0],a[1],a[2],a[3])?

like image 958
JonC Avatar asked Aug 24 '10 16:08

JonC


People also ask

How do you pass multiple arguments to a list in Python?

The * symbol is used to pass a variable number of arguments to a function. Typically, this syntax is used to avoid the code failing when we don't know how many arguments will be sent to the function.

Does Python allow you to pass multiple arguments to a function?

In Python, we can pass multiple arguments using some special symbols. The special symbols are: *args – used to pass a non-keyworded variable number of arguments to a function.

Can we pass list as argument in Python function?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

Is it possible to pass multiple arguments to a function?

Functions can accept more than one argument. When calling a function, you're able to pass multiple arguments to the function; each argument gets stored in a separate parameter and used as a discrete variable within the function.


1 Answers

Using sequence unpacking (thanks to delnan for the name):

zip(*a)
like image 102
Joe D Avatar answered Oct 07 '22 23:10

Joe D