Pre-PEP: Dictionary accumulator methods

Kent Johnson kent37 at tds.net
Sat Mar 19 07:19:57 EST 2005


Brian van den Broek wrote:
> Raymond Hettinger said unto the world upon 2005-03-18 20:24:
> 
>> I would like to get everyone's thoughts on two new dictionary methods:
>>
>>         def appendlist(self, key, *values):
>>             try:
>>                 self[key].extend(values)
>>             except KeyError:
>>                 self[key] = list(values)
>>
> For appendlist, I would have expected
> 
> def appendlist(self, key, sequence):
>     try:
>         self[key].extend(sequence)
>     except KeyError:
>         self[key] = list(sequence)

The original proposal reads better at the point of call when values is a single item. In my 
experience this will be the typical usage:
   d.appendlist(key, 'some value')

as opposed to your proposal which has to be written
   d.appendlist(key, ['some value'])

The original allows values to be a sequence using
   d.appendlist(key, *value_list)

Kent



More information about the Python-list mailing list