Given an empty dictionary:
matrix={}
and another list_of_dictionaries
:
[{(0, 1): 3, (2, 0): 2, (2, 1): 3},
{(2, 0): 3, (0, 0): 3, (2, 1): 3},
{(0, 1): 3, (0, 0): 2, (2, 1): 1},
{(0, 1): 3, (2, 0): 1, (0, 0): 3}]
I want to iterate through a list that consists of indices:
indices=[(0, 0), (0, 1), (2, 0), (2, 1)]
and populate matrix
so each i in in indices is the key, and each dictionary in list_of_dictionaries is the associated value.
I have searched and was only able to find ways to update and populate a dictionary explicitly, but in fact, I don't know what my list_of_dictionaries
will contain, nor what my indices will contain. Is it correct to say, I am looking for a "dynamic" solution? I know
matrix.update(x)
will only take one argument (key or value?), but I want a solution that will take both key and value. Thanks much.
for i in indices:
The result should return matrix and look like:
(0, 0):{(0, 1): 3, (2, 0): 2, (2, 1): 3}
(0, 1):{(2, 0): 3, (0, 0): 3, (2, 1): 3}
(2, 0):{(0, 1): 3, (0, 0): 2, (2, 1): 1}
(2, 1):{(0, 1): 3, (2, 0): 1, (0, 0): 3}