a clean way to define dictionary
Jeremy Yallop
jeremy at jdyallop.freeserve.co.uk
Sat Jun 21 15:02:31 EDT 2003
Alexander Schmolck wrote:
> Skip Montanaro <skip at pobox.com> writes:
>> I don't think it would normally be used that way. Instead, consider you
>> have a preexisting dictionary and want a copy:
>>
>> >>> d1 = {'foo':1, 'bar':'sean'}
>> >>> d2 = dict(**d1)
>> >>> d2 == d1
>> True
>>
>> That's a one-liner where the equivalent
>>
>> d2 = {}
>> d2.update(d1)
>>
>> is a two-liner and likely slower.
>
> Uhm,
>
> d2 = d1.copy() ?
d2 = dict(d1)
Jeremy.
More information about the Python-list
mailing list