Hi guys I'm pretty lost with this simple problem. I have a dictionary and a list of dictionaries in python and I want to loop over the list to add each dictionary to the first dictionary but somehow it just adds the last dictionary with the solution I came up with. I'm using Python 3.6.5
This is what I've tried:
res = []
dictionary = {"id": 1, "name": "Jhon"}
dictionary_2 = [
{"surname": "Doe", "email": "[email protected]"},
{"surname": "Morrison", "email": "[email protected]"},
{"surname": "Targetson", "email": "[email protected]"}
]
for info in dictionary_2:
aux_dict = dictionary
aux_dict["extra"] = info
res.append(aux_dict)
print(res)
What I expect is:
[{'id': 1, 'name': 'Jhon', 'extra': {'surname': 'Doe', 'email': '[email protected]'}},
{'id': 1, 'name': 'Jhon', 'extra': {'surname': 'Morrison', 'email': '[email protected]'}},
{'id': 1, 'name': 'Jhon', 'extra': {'surname': 'Targetson', 'email': '[email protected]'}}]
And this is what I get
[{'id': 1, 'name': 'Jhon', 'extra': {'surname': 'Targetson', 'email': '[email protected]'}},
{'id': 1, 'name': 'Jhon', 'extra': {'surname': 'Targetson', 'email': '[email protected]'}},
{'id': 1, 'name': 'Jhon', 'extra': {'surname': 'Targetson', 'email': '[email protected]'}}]
This is probably a duplicate of some other question but I can't manage to find it