Add tuple to list
Given a tuple (specifically the varargs functions), I want to add a list containing one or more elements and then call another function with the result as a list. So far, the best I've come up with is:
def fn(*args):
l = ['foo', 'bar']
l.extend(args)
fn2(l)
Which, given that Pythons have the usual patience when it comes to this sort of thing, it looks like it takes 2 more lines than it needs to. Is there a more pythonic way?
+2
MHarris
source
to share
2 answers
You can convert a tuple to a list, which will allow you to link it to another list. i.e:
def fn(*args):
fn2(['foo', 'bar'] + list(args))
+9
Brian
source
to share
If your fn2 also accepted varargs, you won't need to create a concatenated list:
def fn2(*l):
print l
def fn(*args):
fn2(1, 2, *args)
fn(10, 9, 8)
produces
(1, 2, 10, 9, 8)
+1
Ned batchelder
source
to share