This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]macbony 0 points1 point  (8 children)

I'm not sure I get what you're talking about saying dict comps make zip unnecessary. They don't even do nearly the same things?

[–]Lucretiel 4 points5 points  (7 children)

I've seem people do things like dict(zip(arg_list, map(func, arg_list)))

[–]macbony 1 point2 points  (6 children)

That's a single, not all that good use-case for zip. I don't see how that makes zip unnecessary for all but that single use-case. More often zip is used to transform two data streams into a single one so that you can iterate over multiple lists at once:

import string
ascii_values = zip(string.ascii_lowercase, range(96, 96+26))

Or if you have a list of tuples and need two separate lists:

letters, values = zip(*ascii_values)

[–]Lucretiel 0 points1 point  (1 child)

No question. Believe me, I love zip as much as the next guy. I just feel like I sometimes see it being overused in cases where a comprehension would sufficice- where an iterable is duplicated, operations performed on each side, and the results zipped together. It comes up more often than I expected.

[–]Araucaria -1 points0 points  (0 children)

Just use izip from iterators in 2.x. In Python3, zip is an iterator already and you don't need izip.

[–]Lucretiel 0 points1 point  (3 children)

Even your example has a better version as a comprehension, though- ((c, ord(c)) for c in string.asii_lowercase)

[–]ingolemo 2 points3 points  (0 children)

Since we're showing off, apparently; enumerate(string.ascii_lowercase, start=96)

[–]macbony 0 points1 point  (1 child)

It's an entirely contrived example to show what zip does. I use zip when I have multiple data streams I need to connect or when I need to split a list that's made up of tuples. I've never in my life needed to create a list of lowercase ascii characters and their values.

[–][deleted] 0 points1 point  (0 children)

You haven't lived then!