0

My requirement is , i want a final list as output which should

participate in loop during concatenation with other list (i got as an output of some other computation),but with below

implementation its not working due to memory referencing ,how can i avoid this. IN PYTHON

Please excuse me for my grammar

test_list=[[0,1],[2,3]]
result_list=[]
for i in range(3):
  result_list=list(result_list)+list(test_list)
  test_list.pop()
  //this below line is affecting the result_list also,how to avoid this
  test_list[0][1]+=1
U13-Forward
  • 69,221
  • 14
  • 89
  • 114

1 Answers1

0

Here is the time deepcopy comes in handy:

from copy import deepcopy
test_list=[[0,1],[2,3]]
result_list=[]
for i in range(3):
  result_list+=deepcopy(test_list)
  test_list.pop()
  test_list[0][1]+=1

But as @RafaelC says it is giving error:

from copy import deepcopy
test_list=[[0,1],[2,3]]
result_list=[]
for i in range(1):
  result_list+=deepcopy(test_list)
  test_list.pop()
  test_list[0][1]+=1

@RafaelC gave another good idea:

test_list=[[0,1],[2,3]]
result_list=[]
for i in range(1):
  result_list=(result_list + test_list ).copy()
  test_list.pop()
  test_list[0][1]+=1
U13-Forward
  • 69,221
  • 14
  • 89
  • 114