Python Forum
assign a value to a dict change all the values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assign a value to a dict change all the values
#5
Appending the variable for the dict just appends a pointer to where the dict is stored (the reference Windspar is talking about). The list items are all pointing to the same place, where the current value of the dict is stored. That's why you are seeing the last version of the dict repeated, because that is what is stored when you display it.

If you want to save the current state to the list, change the append to list_p.append(dict_p.copy()). That will create the shallow copy. Note that this won't work in all cases. Sometimes you need a deep copy.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: assign a value to a dict change all the values - by ichabod801 - Sep-19-2018, 12:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Change row values by other row values from same df JosepMaria 3 2,343 Aug-28-2021, 10:15 PM
Last Post: eddywinch82

Forum Jump:

User Panel Messages

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