r/Python • u/Iqirx • Oct 01 '15
Usage of Python 'zip' (with examples)
http://pavdmyt.com/python-zip-fu/4
Oct 02 '15
There's actually something really cool implied but not explicitly stated: zip(*args) is the inverse of zip.
I actually use this quite often that there's usually a util function called unzip in many of projects.
Also, dict(map(reverse, dict.items())) is my preferred way of flipping dicts, but that's me and I realize this is an article about zip.
3
u/Iqirx Oct 02 '15
Personally, I like to use dict comprehensions for flipping keys-values:
d = dict(red=210, green=105, blue=30) flipped = {v: k for k, v in d.items()}
dict(map(reversed, dict.items()))is also cool!1
3
u/ZetaHunter 3.5.1 async master-race Oct 02 '15
Page goes into infinite redirect loop for me...
1
u/Iqirx Oct 02 '15
Unfortunately, it happens from time to time. Now I'm managing to transfer DNS. Try from different browser, sometimes it helps.
2
u/masasin Expert. 3.9. Robotics. Oct 02 '15
Same here. Try the cached version instead: https://webcache.googleusercontent.com/search?q=cache:BYZ1w6BsEXIJ:pavdmyt.com/python-zip-fu/+&cd=1&hl=en&ct=clnk&gl=jp
11
u/stillalone Oct 01 '15
seq = range(1, 10)
zip(*[iter(seq)]*3)
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]That's actually pretty cool. I remember running into this situation a few times and the stackoverflow page on it suggested something like:
zip(seq[::2],seq[1::2])which seems horribly inefficient compared to using iter.
3
u/dunkler_wanderer Oct 02 '15
You can also use
itertools.repeatinstead of the multiplied list.from itertools import repeat seq = range(1, 10) zip(*repeat(iter(seq), 3))3
u/workingBen Oct 02 '15
In Python 3 you'll need to use list().
seq = range(1, 10) list(zip(*[iter(seq)]*3)) [(1, 2, 3), (4, 5, 6), (7, 8, 9)]2
u/codewarrior0 MCEdit / PyInstaller Oct 02 '15
Maybe I've been using numpy for too long, but the stepped list slices seem perfectly natural to me.
1
u/isarl Oct 02 '15
In NumPy I would opt for:
seq = np.arange(1, 10) seq.shape = (3, 3)...although then you get a 2-array instead of a list of tuples.
2
2
u/mzial Oct 02 '15
Iteration in tuples:
xs = [1, 2, 3, 4, 5]
for a, b in zip(xs, xs[1:]):
print([a, b])Would yield:
[1, 2]
[2, 3]
....
[4, 5]Pretty neat!
1
u/thatguy_314 def __gt__(me, you): return True Oct 03 '15
zip is one of those things that I feel I should use more often, but it has a nasty habit of messing with my head whenever I try to do anything interesting with it. :(
6
u/PyPokerNovice Oct 02 '15 edited Oct 02 '15
One criticism.
zipis different in Python 3 and Python 2. In Python 2zipis not lazy, upon being called it immediately returns the full list. In Python 2, if you want a lazy yielding version, you can useizipfrom theitertoolslibrary. In Python 3 many default functions became lazy. The Python 3 versions ofzip,map, ect exist in Python 2 asizip,imap, ect initertools.I say this because the article opens with docs for the Python 3 version, but the examples are written in Python 2 (
zipis returning a list in the examples, it would just be a "zip object" in Python 3).Either way, great article, a good introduction to the magic of
zipand unpacking.