Python Tutorial

Saturday, July 13, 2013

Python unpacking list, tuples or dictionary as function argument.


def my_func(a, b, c):
    print "a,b,c:",a,b,c

my_func(1,2,3)
my_func( *[1,2,3] ) # unpacking from list
my_func( *(1,2,3) ) # unpacking from tuples
my_func( **{'a':1, 'b':2, 'c':3} ) # unpacking from dictionary

Output:
a,b,c: 1 2 3
a,b,c: 1 2 3
a,b,c: 1 2 3
a,b,c: 1 2 3

0 comments:

Post a Comment