0

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}
iz_
  • 15,923
  • 3
  • 25
  • 40
gurlinthewurld
  • 25
  • 1
  • 1
  • 6

1 Answers1

0

You can try zipping the two lists together, then creating a dictionary out of them, then updating matrix with that dictionary:

matrix = {}
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}]
indices = [(0, 0), (0, 1), (2, 0), (2, 1)]

matrix.update(dict(zip(indices, list_of_dictionaries)))
print(matrix)

Output:

{(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}}

If you must use a for loop:

for i, d in zip(indices, list_of_dictionaries): 
    matrix[i] = d
iz_
  • 15,923
  • 3
  • 25
  • 40
  • Is this a dynamic solution? Are there any other ways to update a dictionary? thanks! – gurlinthewurld Feb 03 '19 at 19:08
  • @gurlinthewurld I'm not sure what you mean by "dynamic," `list_of_dictionaries` and `indices` can be whatever you want. This is the simplest way to update a dictionary without a `for` loop. – iz_ Feb 03 '19 at 19:10
  • I want to say for i in indices, and then update the matrix, also: matrix.update(x) <-- is the x argument key, or value? Thanks! – gurlinthewurld Feb 03 '19 at 19:12
  • @gurlinthewurld If you look at the documentation for `update`, it says that `update` takes in a *dictioinary*, not a key or value. – iz_ Feb 03 '19 at 19:14
  • @gurlinthewurld to update a dictionary using single key-value pairs in a loop, one generally doesn't use `.update`, which takes *another* dctionary as an argument. You would just do `matrix[key] = value` – juanpa.arrivillaga Feb 03 '19 at 19:24
  • @gurlinthewurld I added a for loop solution. However, please note that there is essentially no reason to use a for loop; it's less readable and slower than the one-line `update` method. – iz_ Feb 03 '19 at 19:36