[Tutor] Dictionary Comprehensions

Khalid Al-Ghamdi emailkgnow at gmail.com
Fri Dec 4 21:32:01 CET 2009

Hi everyone!

I'm using python 3.1 and I want to to know why is it when I enter the
following in a dictionary comprehension:

>>> dc={y:x for y in list("khalid") for x in range(6)}

I get the following:
{'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5}

instead of the expected:
{'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5}

and is there a way to get the target (expected) dictionary using a
dictionary comprehension.

note that I tried sorted(range(6)) also but to no avail.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091204/45f2e931/attachment-0001.htm>

More information about the Tutor mailing list