[Python] shortcut?

cmeyers at guardnet.com cmeyers at guardnet.com
Mon May 22 09:55:43 PDT 2006


Oops

This works for counters and stuff but as Horst points out, append is a
modification in place function. Sorry

Chris

> How about
>
>    d[k] = d.get(k,[]).append(v)
>
> I use this quite a bit. The second arg to .get is a default value, if
> missing it's None.
>
> Chris
>
>> Is there a Python shortcut for the following?
>>
>> if d.has_key(k):
>> 	d[k].append(v)
>> else:
>> 	d[k] = [v]
>>
>> I'm keeping a dictionary of key:list pairs and I'm writing the above
>> often.  I think I remember seeing a shortcut to something like this
>> but I can't recall what it was.
>>
>> Thanks,
>> Rob
>> _______________________________________________
>> Python mailing list
>> Python at euglug.org
>> http://www.euglug.org/mailman/listinfo/python
>
>
>
> _______________________________________________
> Python mailing list
> Python at euglug.org
> http://www.euglug.org/mailman/listinfo/python





More information about the Python mailing list