I have one list of dictionary like this:
data = [{"name":"Kane", "age": 29},
{"name":"will", "age": "32"}]
dtat_2 = [ {"Team":"SRH", "Country" :"NZ"},
{"Team":"RCB", "Country" :"WI"}]
Expected output:
data3 = [{"name":"Kane", "age": 29, "Team":"SRH", "Country" :"NZ"},
{"name":"will", "age": "32", "Team":"RCB", "Country" :"WI"}]
How can I do this?
On python >= 3.5 you can zip the lists and unpack them like this:
[{**d1, **d2} for d1, d2 in zip(data, dtat_2)]
# [{'Country': 'NZ', 'Team': 'SRH', 'age': 29, 'name': 'Kane'},
# {'Country': 'WI', 'Team': 'RCB', 'age': '32', 'name': 'will'}]
Another one that should work for any version is in-place update, this updates one of the dictionaries (just fyi).
for d1, d2 in zip(data, dtat_2):
d1.update(d2)
data
# [{'Country': 'NZ', 'Team': 'SRH', 'age': 29, 'name': 'Kane'},
# {'Country': 'WI', 'Team': 'RCB', 'age': '32', 'name': 'will'}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With