dict invert - learning question
Arnaud Delobelle
arnodel at googlemail.com
Sat May 3 16:57:29 EDT 2008
dave <squareswallower at y1a2hoo3.com> writes:
> Hello,
>
> here is a piece of code I wrote to check the frequency of values and
> switch them around to keys in a new dictionary. Just to measure how
> many times a certain key occurs:
>
> def invert(d):
> inv = {}
> for key in d:
> val = d[key]
> if val not in inv:
> inv.setdefault(val, [key])
You can simply write:
inv[val] = [key]
> else:
> inv[val].append(key)
> return inv
>
>
> Using the methods above (I'm just a beginner) could I have written it
> more concisely? Any criticism/critique in the code would be greatly
> appreciated.
Apart from the unnecessary use of setdefault, it looks good to me.
* You could change if 'val not in inv:' to 'if val in inv:' (and swap
the if and else clauses of course) in order to have a positive
condition rather than a negative one
* If you want to use setdefault, you can replace the if .. else
construct by:
inv.setdefault(val, []).append(key)
* You can also iterate over keys and values using the items() or
iteritems() method of dictionaries:
def invert(d):
inv = {}
for key, val in d.iteritems():
inv.setdefault(val, []).append(key)
return inv
--
Arnaud
More information about the Python-list
mailing list