Yucheng Lin
Yucheng Lin

Reputation: 5

How to change the list in list without change whole list in Python

For example: a = [[]]*50 I want only a[2] to change to [2], but if I do a[2] = [2] all the list become to [2], and I don't know how to do that

Upvotes: 0

Views: 38

Answers (1)

Yılmaz edis
Yılmaz edis

Reputation: 163

>>> a = [[]]*50
>>> a
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], 
[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],  
[], [], [], [], [], [], [], [], [], [], [], []]
>>> a[1] = [2]
>>> a
[[], [2], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],  
[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], 
[], [], [], [], [], [], [], [], [], [], [], [], []]
>>> a[2] = [2]
>>> a
[[], [2], [2], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], 
[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], 
[], [], [], [], [], [], [], [], [], [], [], [], []]

I hope I understood you correctly :)

Upvotes: 1

Related Questions