3

I want to add the values of temp list into the main list. I tried to add some values into the temp list and then append the temp list into the main list as following, but it shows always latest values in the main list.

>>> temp =[]
>>> temp.append(123)
>>> temp.append(10)
>>> temp.append(18)
>>> mutR =[]
>>> mutR.append(temp)
>>> print mutR
[[123, 10, 18]]
>>> temp[:]=[]
>>> temp.append(3)
>>> temp.append(4)
>>> temp.append(5)
>>> mutR.append(temp)
>>> print mutR
[[3, 4, 5], [3, 4, 5]]

My expectation is:

>>> print mutR
[[123, 10, 18], [3, 4, 5]] 

but it is [[3, 4, 5], [3, 4, 5]].

jwodder
  • 54,758
  • 12
  • 108
  • 124
Sang Lee
  • 31
  • 1
  • 2

7 Answers7

2

The statement

temp[:] = []

removes all the elements from temp, what you want to do instead is

temp = []

that will create a new empty list and store its reference into temp.

In your code there is only one list object, added twice to mutR, if you add for example

temp.append(99)
print mutR

to your original code you will see [[3, 4, 5, 99], [3, 4, 5, 99]] as answer.

6502
  • 112,025
  • 15
  • 165
  • 265
1

The thing is when you append temp to mutR, mutR contains only reference to temp. Whatever changes are applied to temp, temp in mutR will also change accordingly. So, the solution is to use copy.

>>> temp =[]
>>> temp.append(123)
>>> temp.append(10)
>>> temp.append(18)
>>> mutR =[]
>>> import copy
>>> mutR.append(copy.copy(temp))
>>> print mutR
[[123, 10, 18]]
>>> temp[:]=[]
>>> temp.append(3)
>>> temp.append(4)
>>> temp.append(5)
>>> mutR.append(temp)
>>> print mutR
[[123, 10, 18], [3, 4, 5]]
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
1

Here you GO

>>> temp =[]
>>> temp.append(123)
>>> temp.append(10)
>>> temp.append(18) 
>>> mutR =[temp]
>>> print(mutR)
[[123, 10, 18]]
>>>
>>>
>>> temp=[]
>>> temp.append(3)
>>> temp.append(4)
>>> temp.append(5)
>>> mutR.append(temp)
>>> print(mutR)
[[123, 10, 18], [3, 4, 5]]
0

You're looking for the extend method.

>>> l = [1, 2, 3]
>>> l.extend([4, 5, 6])
>>> print l
[1, 2, 3, 4, 5, 6]
Max Noel
  • 8,810
  • 1
  • 27
  • 35
  • This helps but I want to keep the bracket like this [[1,2,3],[4,5,6]]. I want to use it like this l[0] (=[1,2,3]), l[1] (=[4,5,6]) – Sang Lee Nov 11 '14 at 18:38
0

mutR.append(temp) appends temp to mutR, temp[:]=[] makes temp an empty list again, the temp in mutR also becomes an empty list as it is a reference to temp not a copy.

Then you append three elements to the empty temp and add temp to mutR with mutR.append(temp) so you have two references to temp in your list.

Use mutR.append(temp[:]) # <- copy of temp not a reference initially to add a copy of tempto mutR so later changes to temp won't affect it.

In [6]: mutR.append(temp[:])   # copy temp  
In [7]: temp[:]=[]    
In [8]: temp.append(3)    
In [9]:  temp.append(4)    
In [10]: temp.append(5)    
In [11]: mutR.append(temp)    
In [12]: mutR
Out[12]: [[123, 10, 18], [3, 4, 5]]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

You need to assign temp[]=[] rather than temp[:]=[]

See also : Python list slice syntax used for no obvious reason

Community
  • 1
  • 1
psychemedia
  • 5,690
  • 7
  • 52
  • 84
0

Pretty much nothing in Python makes a copy unless it explicitly says it makes a copy. In particular, append doesn't make a copy; any changes to the object you append will be reflected in the list you appended it to, because the list is holding a reference to the original object.

To avoid problems like this, don't clear out an existing object when you want a new one. Instead, create a new object:

temp = []

instead of

temp[:] = []

I suppose if you really want to clear the original temp list instead of replacing it, you could instead make a copy when you append:

mutR.append(temp[:])
user2357112
  • 260,549
  • 28
  • 431
  • 505