Re:Python Language Features and Tricks
by mildtrepidation on Saturday March 08, 2014 @12:41PM
(#7365754)
When did support for dictionary comprehensions make it into 2.x? I could've sworn it didn't used to work, but I just tried it in the shell and sure enough, it does in 2.7.3.
Re:Python Language Features and Tricks
by maxerickson on Saturday March 08, 2014 @12:49PM
(#7365778)
Wow I didn't know that! I was always doing something like this
mi = dict((v,k) for k,v in m.iteritems())
Re:Python Language Features and Tricks
by nerfhammer on Saturday March 08, 2014 @01:53PM
(#7365955)
more importantly it's wrong - the iteration order of .values() and .keys() is not guaranteed to be consistent
Re:Python Language Features and Tricks
by davidwtbuxton on Saturday March 08, 2014 @02:37PM
(#7366090)
The order of items in the dictionary isn't stable, but the order of .keys() is guaranteed to be the same as the order of .values() (as long as you don't modify the dict in between calling one then the other).
Instead of
Do