Reputation: 423
I have a nested list:
lists = [['q','w','e'],['r','t','y'],['u','i','o']]
And a function:
def func(*iterables)
How call func
with my list? I tried:
func(item for item in lists)
But it doesn't work.
Upvotes: 1
Views: 1309
Reputation: 152667
The item for item in lists
is a generator expression, it's still just "one" parameter. You actually need to unpack your lists
.
Besides using unpacking (*
):
func(*lists)
you could also use a decorator to "utilize" this approach (if you need it often):
def packed(func):
def inner(args):
return func(*args)
return inner
packed(func)(lists)
I recently added such a "decorator" to a package of mine iteration_utilities.packed
:
from iteration_utilities import packed
packed(func)(lists)
Upvotes: 1
Reputation: 856
List comprehension must be in square parenthesis so the syntax is not correct.
func(*lists)
*lists unpacks the list contents so items are separate arguments in your function.
Upvotes: -1
Reputation: 1008
If I am understanding you correctly...
func(*lists)
This basically unzips the list so the contents can be used as parameters for a function.
The * operator is called a "splat" in some other languages. :)
Upvotes: 2