0

I'm working with an api that returns json formatted as a list (of a list) of dictionaries similar to this:

list_of_dictionaries = [
    [{'key1':0},{'key2':1}],
    [{'key1':2},{'key2':3}],
    [{'key1':4},{'key2':5}],
    [{'key1':6},{'key2':7}],
    [{'key1':8},{'key2':9}]
]

I also have a separate list of tuples that contain coordinates:

coordinates_tuple = namedtuple('Coordinates', ['x','y'])
coordinates_list = []
coordinates_list.append(coordinates_tuple((0),(0)))
coordinates_list.append(coordinates_tuple((1),(0)))
coordinates_list.append(coordinates_tuple((1),(1)))
coordinates_list.append(coordinates_tuple((0),(1)))
coordinates_list.append(coordinates_tuple((-1),(-1)))

My goal is to add each coordinates_tuple as a key/value pair within list_of_dictionaries so I created this for loop which seems to achieve the desired output:

for i in range(len(list_of_dictionaries)):
    x = coordinates_list[i].x
    y = coordinates_list[i].y
    coordinates_dictionary = {'x' : x , 'y' : y}

    list_of_dictionaries[i].append(coordinates_dictionary.copy())
    print(list_of_dictionaries[i]) 

#output
#[{'key1': 0}, {'key2': 1}, {'x': 0, 'y': 0}]
#[{'key1': 2}, {'key2': 3}, {'x': 1, 'y': 0}]
#[{'key1': 4}, {'key2': 5}, {'x': 1, 'y': 1}]
#[{'key1': 6}, {'key2': 7}, {'x': 0, 'y': 1}]
#[{'key1': 8}, {'key2': 9}, {'x': -1, 'y': -1}]

Assuming these two lists will always be the same length and sequentially ordered (coordinates_list[0] will match with list_of_dictionaries[0]) - does this approach make sense or is there a better solution?

bck
  • 51
  • 4
  • 1
    Note, what you show is list of lists of dictionaries... – buran Mar 31 '21 at 19:23
  • you are iterating through the len of list of dicts but you are using the index to look for values in coordinates_list. Do you foresee a situation where it can go out of range on the index? – Joe Ferndz Mar 31 '21 at 19:25
  • @JoeFerndz He said "Assuming these two lists will always be the same length" – Barmar Mar 31 '21 at 19:26
  • Also, you are iterating through the list of dicts but also appending to the same list of dicts. I wouldnt recommend that. the loop can get messy – Joe Ferndz Mar 31 '21 at 19:26
  • A better approach is to use `zip()` to loop over the two lists in parallel. – Barmar Mar 31 '21 at 19:27
  • but OP is adding to the list inside the loop so it will never be same – Joe Ferndz Mar 31 '21 at 19:27
  • 1
    @JoeFerndz The list is 2-dimensional. He's adding to the inner lists, not the list he's iterating over. – Barmar Mar 31 '21 at 19:27

2 Answers2

1

Use zip() to process the two lists together.

for coord, d_list in zip(coordinates_list, list_of_dictionaries):
    d_list.append({'x': coord.x, 'y': coord.y})
Barmar
  • 741,623
  • 53
  • 500
  • 612
1
from collections import namedtuple

list_of_dicts = [
    [{'key1':0},{'key2':1}],
    [{'key1':2},{'key2':3}],
    [{'key1':4},{'key2':5}],
    [{'key1':6},{'key2':7}],
    [{'key1':8},{'key2':9}]]

Coordinates = namedtuple('Coordinates', ['x','y'])
coordinates_list = [Coordinates(0, 0), Coordinates(1, 0),
                    Coordinates(1, 1), Coordinates(0, 1),
                    Coordinates(-1, -1)]

for my_list, coord in zip(list_of_dicts, coordinates_list):
    my_list.append(dict(coord._asdict())) # _asdict() will return OrderedDict

print(list_of_dicts)

Also note that you have excessive use of brackets when constructing the namedtuples.

buran
  • 13,682
  • 10
  • 36
  • 61