[Tutor] working with multiple sets

Alan Gauld alan.gauld at btinternet.com
Tue Sep 8 23:26:14 CEST 2009


"Douglas Philips" <dgou at mac.com> wrote 

>>>        for element in value:
>>                 lookup[element] = lookup.get(element, []).append(key)
> # Doug: That doesn't do what you think it does, it won't insert the  
> new list into the dictionary.

Ah, yes I forgot append was one of those annoying python methods 
that doesn't return self...

You need to do it in three lines... One to get the list, 
one to append and one to assign the value...

> # Doug: I think what you want is lookup.setdefault(element,  
> []).append(key)

or use setdfault (which I forgot all about! :-)

Thanks for picking that one up Doug.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list