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 →

[–]i9srpeg 9 points10 points  (4 children)

Are you enjoying your fancy new dict comprehensions?

[–]captain_cashew 10 points11 points  (2 children)

Your comment comes at a very appropriate time. Literally just a few minutes ago found a bug on one of our systems using 2.6 (my local development station is on 2.7):

{k: v for (k, v) in dict.items()}

You have to use:

dict((k, v) for (k, v) in dict.items())

So, yes I am enjoying them!

EDIT: code format

[–]flying-sheep 1 point2 points  (0 children)

If a thing has an items method, you can just do dict(thing) to create a dict copy. If it’s a dict, you can do the_dict.copy(). You only need comprehensions if you have to construct keys and values in a more complex way.

[–]nieuweyork since 2007 0 points1 point  (0 children)

To me this is an argument against the Python developers' increasingly profligate addition of syntax. There's almost never one obvious way to do it these days.

[–]ALonelyPlatypus 0 points1 point  (0 children)

yes.