473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

The voodoo of zip(*someList)

All,

How (and why) does zip(*someList) work?
s = [[1, 2, 3], ['one', 'two', 'three'], ['I', 'II', 'III']]
zip(*s)

[(1, 'one', 'I'), (2, 'two', 'II'), (3, 'three', 'III')]

I've never seen the star '*' outside of function/method definitions
and I've looked in the Python documentation without success. This
syntax is voodoo to me at the moment. I'm stumped.

Thanks,
Stuart
Jul 18 '05 #1
4 4071
Message Drop Box wrote:
All,

How (and why) does zip(*someList) work?
s = [[1, 2, 3], ['one', 'two', 'three'], ['I', 'II', 'III']]
zip(*s)

[(1, 'one', 'I'), (2, 'two', 'II'), (3, 'three', 'III')]

I've never seen the star '*' outside of function/method definitions
and I've looked in the Python documentation without success. This
syntax is voodoo to me at the moment. I'm stumped.

Thanks,
Stuart

Help on built-in function zip:

zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence.
On the asterisk syntax:
http://docs.python.org/tut/node6.htm...00000000000000
http://docs.python.org/ref/calls.html
Basically,

your_sequence = [ 1, 2, 3, 4 ]
zip(*your_sequence)

is equivalent to:

zip(1, 2, 3, 4)

In other words, it allows you to programmatically build an argument
list. Similarly,

your_mapping = { 'cmpfunc': lambda x: x }
some_list = [ 3, 1, 6, 2 ]
some_list.sort(**your_mapping)

is equivalent to:

zip(cmpfunc = lambda x: x)

The zip function takes an item from each of the sequences given as
arguments, and packs them into a tuple, continuing until the sequences
are exhausted.

seq1 = [ 1, 2, 3, 4 ]
seq2 = [ 4, 3, 2, 1 ]
seq3 = "dave"

zip(seq1, seq2) is:
[ (1, 4), (2, 3), (3, 2), (4, 1) ]

zip(seq1, seq2, seq3) is:
[ (1, 4, "d"), (2, 3, "a"), (3, 2, "v"), (4, 1, "e") ]

There is also an itertools.izip which does the same thing in a
generator. HTH,
David.
Jul 18 '05 #2
David Wilson wrote:
In other words, it allows you to programmatically build an argument
list. Similarly,

your_mapping = { 'cmpfunc': lambda x: x }
some_list = [ 3, 1, 6, 2 ]
some_list.sort(**your_mapping)

is equivalent to:

zip(cmpfunc = lambda x: x)


some_list = [ 3, 1, 6, 2 ]
some_list.sort(cmpfunc = lambda x: x)

There's nothing more confusing when you are trying to learn than an
incorrect example. My apologies, lacking sleep here. :)
David.
Jul 18 '05 #3
md****@yahoo.com (Message Drop Box) writes:
How (and why) does zip(*someList) work?
[...]
I've never seen the star '*' outside of function/method definitions
It can also be used in function invocations, where it means pretty
much the reverse of what it means in function definitions.

In a parameter list "*foo" means "collect remaining positional
arguments into a sequence (tuple) called 'foo'". In an argument list,
it means "take the sequence 'foo' and expand it into a set of
positional arguments".

You can "reverse" the **kwds syntax in the same way too.
and I've looked in the Python documentation without success.


Try page 39 of the Python Reference Manual.
Jul 18 '05 #4
Thank you all very much.

Jacek Generowicz <ja**************@cern.ch> wrote
I've never seen the star '*' outside of function/method definitions


It can also be used in function invocations, where it means pretty
much the reverse of what it means in function definitions.

In a parameter list "*foo" means "collect remaining positional
arguments into a sequence (tuple) called 'foo'". In an argument list,
it means "take the sequence 'foo' and expand it into a set of
positional arguments".

You can "reverse" the **kwds syntax in the same way too.
and I've looked in the Python documentation without success.


Try page 39 of the Python Reference Manual.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
38
by: Steven Bethard | last post by:
> >>> aList = > >>> it = iter(aList) > >>> zip(it, it) > > That behavior is currently an accident. >http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.