I was trying to get a nested dictionary comprehension to work after I found out how a nested list comprehension worked. I succesfully created the nested dictionary comprehension, although I'm still confused why the syntax is different. In the nested list comprehension the inner loop is within the outer loop so to say, while for the dictionary the inner loop follows the outer loop.
Example:
alist = [[1,2,3],[4,5,6],[7,8,9]]
print([[[xi, index] for index, xi in enumerate(x) ] for x in alist])
print({xi: index for x in alist for index, xi in enumerate(x)})
[[[1, 0], [2, 1], [3, 2]], [[4, 0], [5, 1], [6, 2]], [[7, 0], [8, 1], [9, 2]]]
{1: 0, 2: 1, 3: 2, 4: 0, 5: 1, 6: 2, 7: 0, 8: 1, 9: 2}
Can someone clarify this difference in syntax, or am I doing something wrong here?