Paul Manta
Paul Manta

Reputation: 31577

How to unpack a list?

Given a function func(*args) and a list, how can I 'unpack' the list such that I pass its contents as separate arguments?

I know I can do func(*thelist), but what I actually want to do is pass along another object, besides the contents of the list; something like this func(someobj, *thelist).

How can I do that?

Upvotes: 0

Views: 190

Answers (1)

Ned Batchelder
Ned Batchelder

Reputation: 375594

Your code will work exactly as you typed it.

def foo(*mylist):
    bar("first", *mylist)

def bar(*vals):
    print "|".join(vals)

foo("a","b")

will print:

first|a|b

Upvotes: 4

Related Questions