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
#3
List are reference variables. They are always pass as reference.
When you assign one list to all variables.
It all point to the same list.

Examples
import numpy as np
td =[np.Inf, 2, 3]
a = {}

for k in range(10):
    a[k] = td # point to one list

a[0][0] = 1
td[1] = 10
for i in a.items():
    print(i)

b = {}
for k in range(10):
    b[k] = td[:] # shallow copy

b[0][0] = 2
td[1] = 0
print()
for i in b.items():
    print(i)
a = [1, 2, 3]
b = a
c = a[:] # shallow copy

print(a)
print(b)
print(c)
print()

a[1] = 6
b[0] = 5
c[2] = 7

print(a)
print(b)
print(c)
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: assign a value to a dict change all the values - by Windspar - Sep-18-2018, 05:59 PM

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