Bottom Page

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'}
Quote
#2
You need to read about mutable and immutable objects in Python.

Dictionaries are mutable objects, so they could be changed in-place:

single_alien = {'color': 'grey', 'points': 8, 'speed': 'ultraslow'}

print(id(single_alien)) # print unique identifier of the object single_alien
# assume, that returned by `id` value is 12345

single_alien['color'] = 'green'
single_alien['points'] =  5
single_alien['speed'] = 'slow'

print(id(single_alien)) # lets print id again, after changes... 
# it is 12345! # So, the object was changed in-place, this is mutability...
aliens.append(single_alien)
 
single_alien['color'] = 'blue'
single_alien['points'] =  6
single_alien['speed'] = 'medium'

print(id(single_alien))
# it is still...  12345!
aliens.append(single_alien)
So, each time you appending single_alien to aliens, Python just duplicates references to the object single_alien;

for alien in aliens:
 print(id(alien))
 # will give the same values 
Each time you assigning a new dictionary to the single_alien,
internally, a new python object is created. You can check it with id.
ids will be different...

single_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
print(id(single_alien))
# eg. 123456
aliens.append(single_alien)

single_alien = {'color': 'blue', 'points': 6, 'speed': 'medium'}
print(id(single_alien))
# eg. 123234  123234!=123456, i.e. another object!
aliens.append(single_alien)
Quote
#3
(Apr-03-2018, 12:39 AM)scidam Wrote: You need to read about mutable and immutable objects in Python.
Dictionaries are mutable objects, so they could be changed in-place:

Thank you very much, Tim
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Appending to list not working and causing a infinite loop eiger23 8 149 Oct-10-2019, 03:41 PM
Last Post: eiger23
  Appending a list in a class from a callback function snizbatch 5 185 Sep-01-2019, 06:27 AM
Last Post: snizbatch
  List of dictionaries: use key-value instead of index? dn237 6 491 Jun-03-2019, 05:52 PM
Last Post: Yoriz
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 700 May-29-2019, 02:27 AM
Last Post: heiner55
  Dictionary adds an unexpected list erina 1 290 Mar-14-2019, 01:13 PM
Last Post: ichabod801
  Help to flatten list of nested dictionaries shawbapmp 4 669 Feb-25-2019, 10:18 PM
Last Post: shawbapmp
  Unexpected round behavior pythonCoder 1 303 Feb-19-2019, 02:39 PM
Last Post: marienbad
  Help with Dictionaries and List of Lists artblinked 7 404 Feb-14-2019, 05:07 PM
Last Post: artblinked
  Unexpected ininite loop behavior RedSkeleton007 4 381 Jan-27-2019, 05:32 PM
Last Post: aakashjha001
  List of dictionaries bob_for_practice 2 388 Jan-20-2019, 09:03 AM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)