[Python-Dev] Re: A small proposed change to dictionaries' "get" method
Matthias
matthias_w at my-deja.com
Thu Aug 3 07:38:51 EDT 2000
In article <3989454C.5C9EF39B at lemburg.com>,
"M.-A. Lemburg" <mal at lemburg.com> wrote:
<snip>
>> "Barry A. Warsaw" wrote:
>> d = UserDict()
>> word = 'hello'
>> d.get(word, [])
>> []
>> d.put(word, []).append('world')
>> d.get(word)
>> ['world']
>> d.put(word, []).append('gareth')
>> d.get(word)
>> ['world', 'gareth']
>>
<snip>
> The following one-liner already does what you want:
>
> d[word] = d.get(word, []).append('world')
>
> ... and it's in no way more readable than your proposed
> .put() line ;-)
>
> --
> Marc-Andre Lemburg
Sorry Marc,
since append does not return anything your proposal is not possible
try this:
>>> word='aword'
>>> d={}
>>> print d.get(word,[]).append(word)
None
>>>
:-)
Matthias
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list