Unpacking sequences and keywords in one function call

Noah Rawlins noah.rawlins at comcast.net
Mon Nov 13 20:42:24 EST 2006


ram wrote:
> Stupid question #983098403:
> 
> I can't seem to pass an unpacked sequence and keyword arguments to a
> function at the same time. What am I doing wrong?
> 
> def f(*args, **kw):
>     for a in args:
>         print 'arg:', a
>     for (k,v) in kw.iteritems():
>         print k, '=', v
> 
>>>> f(1,2)
> arg: 1
> arg: 2
> 
>>>> f(*[1,2])
> arg: 1
> arg: 2
> 
>>>> f(1,2, a=1)
> arg: 1
> arg: 2
> a = 1
> 
>>>> f(*[1,2], a=1)
>     File "<stdin>", line 1
>       f(*[1,2], a=1)
>                   ^
>     SyntaxError: invalid syntax 
> 
> Thanks,
> Rick
> 

I don't know if it's because there's some potential ambiguity (that I'm 
not seeing), but yeah, you just can't do that.  This should work though...

 >>> f(*[1, 2], **{'a':1})

noah



More information about the Python-list mailing list