1
import copy
tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]
actualtable=[]

actualtable =copy.copy(tableData)
tableData[0][0]='banana'


print(tableData)
print(actualtable)

Why does both the lists tableData and actualtable point to the same structure. Can someone help me out!

Screenshot

norie
  • 9,609
  • 2
  • 11
  • 18
Siva Kumar
  • 23
  • 2
  • that screenshot looks pretty cool, could you tell me where it's from? btw, if you replace `[Screenshot][1]` by `![Screenshot][1]`, you can embed the screenshot instead of only linking to it. – Silly Freak May 12 '21 at 06:37

1 Answers1

0

you are using a shallow copy, use deepcopy to get a different reference to list elements:

docs: https://docs.python.org/3/library/copy.html

copy.copy(x)

Return a shallow copy of x.

copy.deepcopy(x[, memo])

Return a deep copy of x.

so in your code just replace copy.copy with copy.deepcopy:

actualtable = copy.deepcopy(tableData)

and you'll get two entirely different list of lists

Ammar
  • 1,305
  • 2
  • 11
  • 16