a clean way to define dictionary

Skip Montanaro skip at pobox.com
Wed Jun 18 22:23:01 CEST 2003

    Alexander> Maybe I'm a bit blinkered, but right now I can't see how

    Alexander>     dict(foo=1, bar='sean')

    Alexander> is so much better/more convinient than

    Alexander>     {'foo':1, bar:'sean'}

    Alexander> that it justifies forcing people to learn a new redundant and
    Alexander> less general dictionary creation syntax that at least hinders
    Alexander> customizing dictionary instantiation like in

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

That's a one-liner where the equivalent

    d2 = {}

is a two-liner and likely slower.


More information about the Python-list mailing list