4 ms·
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():
by stillinbeta 14y ago
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():
- js2 14y agoOr iteritems() for an iterable.
- pcote 14y agoI 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()}
- andreasvc 14y agoWell 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).
- andreasvc 14y agoErm, you can leave out 'in most circumstances' and 'default'; this the way they always work.