News for hackers. Stuff that matters.
Re:Python: The Dictionary Playbook by stillinbeta on Wednesday January 02, 2013 @09:45PM (#5000804)
In most circumstances for (key, val) in data won't work, as the default dictionary iterable only contains the keys. You want for key, val in data.items():
Re:Python: The Dictionary Playbook by pcote on Wednesday January 02, 2013 @10:13PM (#5000862)
I use dictionary comprehensions personally but I have mixed feelings about the syntax. It looks too much like set comprehensions on first glance. Compare the following to see what I mean.

myset = {x for x in "This is my stuff".split()}

mydict = {x:len(x) for x in "This is my stuff".split()}

Re:Python: The Dictionary Playbook by andreasvc on Thursday January 03, 2013 @03:04AM (#5001417)
Well I see what you mean but then again a comma looks very much like a period ...

The advantage of this syntax is that : unambiguously introduces a key: value pair, whereas (key, value) could also occur in a list comprehension (e.g., by accident).

Re:Python: The Dictionary Playbook by js2 on Wednesday January 02, 2013 @09:51PM (#5000814)
Or iteritems() for an iterable.
Re:Python: The Dictionary Playbook by andreasvc on Thursday January 03, 2013 @03:07AM (#5001423)
Erm, you can leave out 'in most circumstances' and 'default'; this the way they always work.
Re:Python: The Dictionary Playbook by andylei on Thursday January 03, 2013 @05:20AM (#5001715)
well,

    (key, val) in data 
works if

    data = {(1,2) : 3}
Re:Python: The Dictionary Playbook by andreasvc on Thursday January 03, 2013 @12:38PM (#5003446)
Well but then the variable names are just wrong.