Python Forum
Unexpected behavior appending to a List of Dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected behavior appending to a List of Dictionaries
#1
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 :

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)
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'}
Reply


Messages In This Thread
Unexpected behavior appending to a List of Dictionaries - by Nomad - Apr-03-2018, 12:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 2 209 May-01-2024, 07:16 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 2 611 Apr-29-2024, 04:38 PM
Last Post: Calab
  Access list of dictionaries britesc 4 1,105 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  behavior list of lists roym 5 2,146 Jul-04-2021, 04:43 PM
Last Post: roym
  function that returns a list of dictionaries nostradamus64 2 1,784 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Time.sleep: stop appending item to the list if time is early quest 0 1,893 Apr-13-2021, 11:44 AM
Last Post: quest
  Reading and appending list MrSwiss 1 1,750 Mar-01-2021, 09:01 AM
Last Post: Serafim
  convert List with dictionaries to a single dictionary iamaghost 3 2,887 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Appending list Trouble Big Time Milfredo 7 3,180 Oct-01-2020, 02:59 AM
Last Post: Milfredo
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,324 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020