0

I have two lists of rows, each with two rows, like this:

list1 = [{'a': 'foo', 'b': 'bar', 'c': 'baz'}, {'d': 'qux', 'e': 'quux', 'f': 'quuz'}]
list2 = [{'g': 'corge', 'h': 'grault', 'i': 'garply'}, {'j': 'waldo', 'k': 'fred', 'l': 'plugh'}]

I would like to join the two lists so that row 1 from list 1 is joined with row 1 from list 2 and row 2 from list 1 is joined with row 2 from list 2, like this:

final_list = [{'a': foo, 'b': bar 'c': baz, 'g': corge, 'h': grault, 'i': garply}, {'d': qux, 'e': quux, 'f': quuz, 'j': waldo, 'k': fred, 'l': plugh}]

I have tried:

final_list = [[i, j] for i,j in zip(list1, list2)]

But it doesn't quite join them correctly. Instead, it produces:

[{'a': foo, 'b': bar 'c': baz}, {'g': corge, 'h': grault, 'i': garply}], [{'d': qux, 'e': quux, 'f': quuz}, {'j': waldo, 'k': fred, 'l': plugh}]

I would like to join these lists of rows so that I can then loop through them with Jinja on an HTML page. How can I resolve this issue?

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
acodeaday
  • 47
  • 5
  • Does this answer your question? [How do I merge two dictionaries in a single expression (take union of dictionaries)?](https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-take-union-of-dictionari) – wovano May 04 '22 at 05:47

3 Answers3

1

Your list comprehension should be creating a new dictionary, rather than a list of dictionaries, for each element in the result from zip():

list1 = [
 {'a': 'foo', 'b': 'bar', 'c': 'baz'},
 {'d': 'qux', 'e': 'quux', 'f': 'quuz'}
]

list2 = [
 {'g': 'corge', 'h': 'grault', 'i': 'garply'},
 {'j': 'waldo', 'k': 'fred', 'l': 'plugh'}
]

# Can also use "x | y" in place of "{**x, **y}" if on Python 3.9+
result = [{**x, **y} for x, y in zip(list1, list2)]
print(result)

This outputs:

[
 {'a': 'foo', 'b': 'bar', 'c': 'baz', 'g': 'corge', 'h': 'grault', 'i': 'garply'},
 {'d': 'qux', 'e': 'quux', 'f': 'quuz', 'j': 'waldo', 'k': 'fred', 'l': 'plugh'}
]
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • 1
    `x | y` has a minor advantage in certain cases, in that it will preserve the types involved; if the dictionaries are actually a `dict` subclass, you'll produce the subclass as the result (assuming it's not implemented terribly), where PEP 448 unpacking generalizations always produce a pure `dict`. – ShadowRanger May 04 '22 at 05:47
  • Thank you! This worked! Although I don't fully understand what happened here, do you know where I could go or what I could read to understand this function better? – acodeaday May 04 '22 at 06:40
  • Which function? Several are used in this answer. – BrokenBenchmark May 04 '22 at 15:54
1

You can actually use the update function which can be used to merge two python dictionaries and is very simple and easy to use.

a = [
    {"a": "foo", "b": "bar", "c": "baz"},
    {"d": "qux", "e": "quux", "f": "quuz"},
]

b = [
    {"g": "corge", "h": "grault", "i": "garply"},
    {"j": "waldo", "k": "fred", "l": "plugh"},
]

for i in range(len(a)):
    a[i].update(b[i])
print(a)

The output is as

[{'a': 'foo', 'b': 'bar', 'c': 'baz', 'g': 'corge', 'h': 'grault', 'i': 'garply'}, {'d': 'qux', 'e': 'quux', 'f': 'quuz', 'j': 'waldo', 'k': 'fred', 'l': 'plugh'}]
Shivam Anand
  • 952
  • 1
  • 10
  • 21
0

Your items are dictionary objects…

You can create a new dict out of the two… the keys of the last dict will overwrite the values if any are the same. The below is using the dict unpacking operator **.

final_lst = [{**d1, **d2} for d1, d2 in zip(lst1, lst2)]

There are other ways as well. Removed first example as it wasn’t correct.

Jarvis
  • 669
  • 4
  • 10