Apr-03-2018, 12:08 AM
Hi all
I am getting unexpected behavior when appending a Dictionary to a List of Dictionaries.
If I define the dictionary variable in one go, and then append it to the list - it is successful
If I define the dictionary variable step by step with a single key and value each time and then append it to the list, it does not work as expected. In addition, defining the dictionary variable step by step seems to somehow change the List of Dictionaries entry too.
Could you please help advise what I am doing wrong in the second case. This is my first post, so please let me know if I am not posting correctly, too.
Thank you
The code is :
The output from the two scenarios is :
this works
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'blue', 'points': 6, 'speed': 'medium'}
{'color': 'orange', 'points': 7, 'speed': 'fast'}
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
this doesn't work
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
I am getting unexpected behavior when appending a Dictionary to a List of Dictionaries.
If I define the dictionary variable in one go, and then append it to the list - it is successful
If I define the dictionary variable step by step with a single key and value each time and then append it to the list, it does not work as expected. In addition, defining the dictionary variable step by step seems to somehow change the List of Dictionaries entry too.
Could you please help advise what I am doing wrong in the second case. This is my first post, so please let me know if I am not posting correctly, too.
Thank you
The code is :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
print ( "this works" ) aliens = [] single_alien = { 'color' : 'green' , 'points' : 5 , 'speed' : 'slow' } aliens.append(single_alien) single_alien = { 'color' : 'blue' , 'points' : 6 , 'speed' : 'medium' } aliens.append(single_alien) single_alien = { 'color' : 'orange' , 'points' : 7 , 'speed' : 'fast' } aliens.append(single_alien) single_alien = { 'color' : 'grey' , 'points' : 8 , 'speed' : 'ultraslow' } aliens.append(single_alien) for alien in aliens: print (alien) print ( "this doesn't work" ) aliens = [] single_alien[ 'color' ] = 'green' single_alien[ 'points' ] = 5 single_alien[ 'speed' ] = 'slow' aliens.append(single_alien) single_alien[ 'color' ] = 'blue' single_alien[ 'points' ] = 6 single_alien[ 'speed' ] = 'medium' aliens.append(single_alien) single_alien[ 'color' ] = 'orange' single_alien[ 'points' ] = 7 single_alien[ 'speed' ] = 'fast' aliens.append(single_alien) single_alien[ 'color' ] = 'grey' single_alien[ 'points' ] = 8 single_alien[ 'speed' ] = 'ultraslow' aliens.append(single_alien) for alien in aliens: print (alien) |
this works
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'blue', 'points': 6, 'speed': 'medium'}
{'color': 'orange', 'points': 7, 'speed': 'fast'}
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
this doesn't work
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}
{'color': 'grey', 'points': 8, 'speed': 'ultraslow'}