dict.fromkeys strange behavior

Chris Rebert clp2 at rebertia.com
Mon Dec 7 04:14:05 EST 2009


On Mon, Dec 7, 2009 at 12:42 AM, Tsviki Hirsh <tsviki.hirsh at gmail.com> wrote:
> Dear list,
> I'm trying to create a dictionary from set of keys and values using
> dict.fromkeys
> When I type:
>>>>dict.fromkeys('a',50)
> the output is:
> {'a': 50}
> This is fine, but when I try to set the same value to a different name key:
>>>>dict.fromkeys('at',50)
> the output now is:
> {'a': 50, 't': 50}
> This is obviously not what I wanted.
> What are the alternatives?

>>> dict.fromkeys(['at'],50)
{'at': 50}

Remember that strings are collections too (of individual characters).

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list