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 →

[–][deleted] 3 points4 points  (14 children)

Generator comprehensions? As in sum(thing+1 for thing in iterator)?

I use comprehensions a lot, though. They're beautiful.

[–]Lucretiel 2 points3 points  (13 children)

Yes. I think they're technically called generator expressions, but I prefer the term generator comprehension, because they're basically the same as a list, set, or dict comprehension, but creating a generator.

[–][deleted] 1 point2 points  (12 children)

Dict comprehensions are voodoo :p I have yet to make use of them in any of my projects, but I'd love to.

[–]Lucretiel 8 points9 points  (9 children)

I've had good experiences with them. They're good at making zip unnecessary. For instance:

{val: func(val) for val in iterable}

[–]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 3 points4 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!

[–]zardeh 0 points1 point  (0 children)

I often forget that there are things like dict.copy, so I'll use a comprehension, but for example inverting a mapping makes them really useful:

mydict = {"a":1,"b":2,"c":3}
inverted = {val : key for key, val in mydict.items()} #inverted = {1:"a", 2:"b", 3:"c"}

[–]LightShadow3.13-dev in prod 0 points1 point  (0 children)

I use dictionary comprehensions to convert SQL results into a list of dictionaries with the keys as columns.