[Python-Dev] dict.addlist()

Boris Boutillier Boris.Boutillier at arteris.net
Tue Jan 20 09:34:28 EST 2004


This also mean that we add :
    d.addset(k,v)               d.setdefault(k,Set()).add(v)
    d.adddict(k,subk,v)     d.setdefault(k,{})[subk]=v

I think we can't add a new method for each python datatype ..


Raymond Hettinger wrote:

>Here is anidea to kick around:
>
>Evolve and eventually replace dict.setdefault with a more specialized 
>method that is clearer, cleaner, easier to use, and faster.
>
>       d.addlist(k, v)   would work like   d.setdefault(k, []).append(v)
>       
>
>Raymond Hettinger
>
>
>  
>
>>>>d = dict()
>>>>for elem in 'apple orange ant otter banana asp onyx boa'.split():
>>>>        
>>>>
>...     k = elem[0]
>...     d.addlist(k, elem)
>...
>  
>
>>>>d
>>>>        
>>>>
>{'a': ['apple', 'ant', 'asp'], 'b': ['banana', 'boa'], 'o': ['orange',
>'otter',
>'onyx']}
>  
>
I'm sure here you'll be more interested in a addset rather than addlist :).

>
>bookindex = dict()
>for pageno, page in enumerate(pages):
>    for word in page:
>        bookindex.addlist(word, pageno)
>
>
>_______________________________________________
>Python-Dev mailing list
>Python-Dev at python.org
>http://mail.python.org/mailman/listinfo/python-dev
>Unsubscribe: http://mail.python.org/mailman/options/python-dev/boris.boutillier%40arteris.net
>  
>






More information about the Python-Dev mailing list