I wanted to create a list that contains x amount of dictionaries all containing the same keys but with different values that's made in a for loop:
Something like
[{'name': Brenda, 'Age': 22, 'Sex': Female},
{'name': Jorda, 'Age': 32, 'Sex': Male},
{'name': Richard, 'Age': 54, 'Sex': Male}]
My code is this:
people = []
person = {}
humans = gethumans()
for human in humans:
number_people, people_data = People.data()
person['name'] = human.name
person['age'] = human.age
person['Sex'] = human.name
people.append(person)
My output is something like this:
[{'name': Richard, 'Age': 54, 'Sex': Male},
{'name': Richard, 'Age': 54, 'Sex': Male}
{'name': Richard, 'Age': 54, 'Sex': Male}]
Since the dictionary values are getting replaced and not added and it's just appending the same dicitnary. How can I get around this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…