dict invert - learning question

dave squareswallower at yahoo.com
Sat May 3 17:12:54 EDT 2008


thanks Duncan and Arnaud.

I'm learning Python from the "How to Think Like a Python Programmer" 
book by Allen Downey.  My first try used the "inv[val] = [key]" and 
then the next problem was to incorporate the "D.setdefault(...)" method.

Thank you for your help.  I'm always amazed how kind people are in this group.

On 2008-05-03 14:57:29 -0600, Arnaud Delobelle <arnodel at googlemail.com> said:

> 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





More information about the Python-list mailing list