On 12/4/2009 12:32 PM Khalid Al-Ghamdi said...
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)}

Try breaking this into pieces...

First see what [(x,y) for y in in list("khalid") for x in range(6)]
gets you, then see how that fits into dict().

To get where you want, take a look at zip'ing the two lists.

(II don't remember -- s zip still in 3.1?)

Emile



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.

thanks

------------------------------------------------------------------------

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to