I am trying to create a dictionary where values are obtained from iterating a list. The keys are in the tuple format (i,j).
e.g. if i = 3, j = 3, the keys are
(1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3).
If my list is as follows,
lst = list(range(1, 10))
How do I obtain a dictionary like this?
{(0, 0): 1, (0, 1): 2, (0, 2): 3, (1, 0): 4, (1, 1): 5, (1, 2): 6, (2, 0): 7, (2, 1): 8, (2, 2): 9}
I have tried:
dict = {(i,j): d for d in lst for i in range(1, i+1) for j in range(1, j+1)}
but the values of my dictionary are strictly the last value of lst
. This is my output instead:
>>> {(0, 0): 9, (0, 1): 9, (0, 2): 9, (1, 0): 9, (1, 1): 9, (1, 2): 9, (2, 0): 9, (2, 1): 9, (2, 2): 9}