r/Python • • May 30 '19

Dear python2 user

Post image
1.9k Upvotes

275 comments sorted by

View all comments

56

u/captain_cashew May 30 '19

My company is moving from 2.6 to 2.7! Yay!

10

u/i9srpeg May 30 '19

Are you enjoying your fancy new dict comprehensions?

11

u/captain_cashew May 30 '19 edited May 31 '19

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

2

u/flying-sheep May 31 '19

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.