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
#7
The list is a reference too, just like the dictionary. A shallow copy of the list gives you all the items in the list. But some of those items can be references too. A shallow copy will just give you those references.

>>> x = [1, 2, 3]
>>> y = [x, 4, 5]
>>> y
[[1, 2, 3], 4, 5]
>>> z = y[:]  # shallow copy
>>> y[1] = 6
>>> y
[[1, 2, 3], 6, 5]
>>> z
[[1, 2, 3], 4, 5] # integers that are values don't have changes affect the shallow copy
>>> x[1] = 7
>>> z
[[1, 7, 3], 4, 5] # lists that are references do have changes affect the shallow copy
A deep copy would make an independent copy of everything in the list.
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-20-2018, 01:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Change row values by other row values from same df JosepMaria 3 2,337 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