I thought about a small python exercise today, how would someone dynamically update the dictionary values in array from a tuple.
I have the following dictionary:
people = [
{"first_name": "Jane", "last_name": "Watson", "age": 28},
{"first_name": "Robert", "last_name": "Williams", "age": 34},
{"first_name": "Adam", "last_name": "Barry", "age": 27}
]
now i have a list of tuple:
new_names = [("Richard", "Martinez"), ("Justin", "Hutchinson"), ("Candace", "Garrett")]
I have tried this approach:
for person in people:
for name in new_names:
person["first_name"] = name
person["last_name"] = name
but its wrong because it give me the same values everywhere
[{'age': 28,
'first_name': ('Candace', 'Garrett'),
'last_name': ('Candace', 'Garrett')},
{'age': 34,
'first_name': ('Candace', 'Garrett'),
'last_name': ('Candace', 'Garrett')},
{'age': 27,
'first_name': ('Candace', 'Garrett'),
'last_name': ('Candace', 'Garrett')}]
How can i update the first_name
and last_name
with the tuples data above?