News for hackers. Stuff that matters.
Re:Python Language Features and Tricks by lqdc13 on Saturday March 08, 2014 @12:09PM (#7365670)
zip to unzip a dict is a very slow approach to do it

Instead of

    mi = dict(zip(m.values(), m.keys()))
Do

    mi = {v: k for (k, v) in m.iteritems()}
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)
Re:Python Language Features and Tricks by chingjun on Sunday March 09, 2014 @01:30PM (#7369090)
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).

http://docs.python.org/2/library/stdtypes.html#dict.items