Apologies in advance, I loathe to ask such a newb question (this is the first language I'm learning) but there's clearly a gap in my understanding, and I can't find a link to any content which solves the issue I'm experiencing.
The below code defines 3 dictionaries, and an empty list. I then run a for loop to populate the list with 30 "Slow Aliens".
I then run another loop on the first 10 entries in the list in order to update that particular instance to a "Fast Alien" dictionary. However, I seem to be replacing every dictionary in the list.
Any help pointing me to some articles which discuss/solve/educate me further would be appreciatively received.
slow_alien = {
"Name": "Slow Alien",
"Colour": "Green",
"Points": 10,
"X Position": 15,
"Y Position": 20,
"Current Speed": "Slow",
"Height": 1.8,
}
medium_alien = {
"Name": "Medium Alien",
"Colour": "Yellow",
"Points": 15,
"X Position": 30,
"Y Position": 20,
"Current Speed": "Medium",
"Height": 2.2,
}
fast_alien = {
"Name": "Fast Alien",
"Colour": "Red",
"Points": 20,
"X Position": 45,
"Y Position": 20,
"Current Speed": "Fast",
"Height": 2.6,
}
aliens = []
for alien in range(30):
aliens.append(slow_alien)
for alien in aliens[:10]:
if alien == slow_alien:
alien.update(fast_alien)
for alien in aliens:
for key, value in alien.items():
print(str(key) + ": " + str(value))
print("")
print("The aliens list contains " + str(len(aliens)) + " dictionaries")
Thanks,
Carl.