Python nested dict comprehension with sets

Posted by Jasie on Stack Overflow See other posts from Stack Overflow or by Jasie
Published on 2011-01-09T11:45:54Z Indexed on 2011/01/09 11:53 UTC
Read the original article Hit count: 285

Can someone explain how to do nested dict comprehensions?

>> l = [set([1, 2, 3]), set([4, 5, 6])]
>> j = dict((a, i) for a in s for i, s in enumerate(l))
>> NameError: name 's' is not defined

I would have liked:

>> j
>> {1:0, 2:0, 3:0, 4: 1, 5: 1, 6: 1}

I just asked a previous question about a simpler dict comprehension where the parentheses in the generator function were reduced. How come the s in the leftmost comprehension is not recognized?

© Stack Overflow or respective owner

Related posts about python

Related posts about dictionary