469,327 Members | 1,226 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

zip asterisk syntax

Hello, I have been reading text processing in python and in the appendix
the author describes:
sides = [(3, 4), (7, 11), (35, 8)]
zip(*zip(*sides))


what is this asterisk-list syntax called? Any suggestions for finding more
information about it? Thanks. -Garett
Jul 18 '05 #1
1 1979
garett wrote:
Hello, I have been reading text processing in python and in the appendix
the author describes:
sides = [(3, 4), (7, 11), (35, 8)]
zip(*zip(*sides))
what is this asterisk-list syntax called? Any suggestions for finding more
information about it? Thanks. -Garett


It's called the "extended function call" syntax. Basically what it does
is take the items of a list and use them as arguments to a function.
Let's take the example above:
sides = [(3, 4), (7, 11), (35, 8)]
zip(*zip(*sides))
is the same as:
zip(*zip((3,4),(7,11),(35,8)))
zip() mangles its arguments in such a way that the above call is
equivalent to this:
zip(*[(3,7,35),(4,11,8)])
which is the same as:
zip((3,7,35),(4,11,8))

which evaluates to [(3,4),(7,11),(35,8)].

Back on topic, the dictionary equivalent of * is **; that is, ** takes the
key-value pairs from a dictionary and uses them as arguments to a
function. * and ** must appear after other arguments to the function, and
you may only have at most one each of * and **.

There's another context other than function calls in which * and ** can be
used, and that's function definitions:
def(a,b=5,*c,**d): pass


will pack any extra arguments (beyond a and b) into list c and dictionary
d.

For (very) detailed information, see http://docs.python.org/ref/calls.html
and http://docs.python.org/ref/function.html.

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by sarmin | last post: by
1 post views Thread by sarmin kho | last post: by
2 posts views Thread by Marco | last post: by
2 posts views Thread by Evan | last post: by
reply views Thread by selvarani | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.