Creating a list of dictionaries with same keys? [duplicate]

When you append the dictionary person to the list people you are just appending a reference to the dictionary to the list, so the list ends up containing just references to the SAME dictionary.

Since each time through the loop you overwrite the dictionary with new values, at the end the list contains just references to the last person you appended.

What you need to do is create a new dictionary for every person, for example:

for human in humans:
    number_people, people_data = People.data()
    person = dict()
    person['name'] = human.name
    person['age'] = human.age
    person['Sex'] = human.name
    people.append(person)

Leave a Comment