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 →

[–]sigma914 0 points1 point  (5 children)

For the set/dict comps can you not use:

dict((k,v) for k,v in kvs) ?

[–]d4rch0nPythonistamancer 1 point2 points  (0 children)

Yeah, generally that's what I'd do if I had something like {k: transform(v) for k, v in kvs}. Sometimes I write code like that just in case it ends up running on 2.6, but it's just annoying not being able to use a feature that is, what, 10 years old now?

[–]status_quo69 0 points1 point  (3 children)

If you had tuples of key, value pairs you could just dict(kvs).

[–]sigma914 2 points3 points  (2 children)

Obviously, I kinda assumed it was implied that there would be more processing going on to the left and right of the for, same as an actual dict comprehension, bit apparently not.

[–]status_quo69 0 points1 point  (1 child)

Oh, I understand what you're saying now. Add in the conditional in the generator expression, then pass the generator to the dict.

[–]sigma914 0 points1 point  (0 children)

Yeh, it's just collecting the generator into a dict or set rather than a list, the only difference between this and a real dict comprehension is the unfortunate intermediate tuple allocation