0

I have a list of lists in Python, such as

colours = [['1', '2', '3'], ['1', '2', '3'], ['1', '2', '3']]

All three lists are the same at the beginning. I want to remove the first 1 from the first list so that I have

colours = [['2', '3'], ['1', '2', '3'], ['1', '2', '3']]

but whatever I tried will remove the 1 from every list.

I tried doing colours[0].remove('1') but then the result is

colour = [['2', '3'], ['2', '3'], ['2', '3']]

How can I do it?

I have defined colours as:

list = ['1', '2', '3']
for i in range(3):
    colours.append(list)
Timus
  • 10,974
  • 5
  • 14
  • 28
  • Is that really how you define your list or have you created a list with multiple references to the same list? – Sayse Mar 03 '22 at 14:54
  • Are you sure about your result ? With your code my output is `[['2', '3'], ['1', '2', '3'], ['1', '2', '3']]` – Titouan L Mar 03 '22 at 14:54
  • So I have created it like this: ``` list = ['1','2','3'] for i in range(3): colours.append(list)``` – Bogdan Rotaru Mar 03 '22 at 14:56
  • The question is about how you created `colors`. See https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly which is probably what causes your problem. – Thierry Lathuille Mar 03 '22 at 14:57
  • 1
    @BogdanRotaru Ok first of all using list as a list name is not a good idea because list() is also the function to make lists. Also by appending that list you might have added the SAME list 3 times to the colours list so changing one changes all as they refers to the same object. – haxor789 Mar 03 '22 at 14:59
  • See the duplicate for more detailed explanations. Short answer: do `colors = [['1', '2', '3'] for _ in range(3)]` to create a list of 3 distinct sublists. – Thierry Lathuille Mar 03 '22 at 15:05

3 Answers3

0

You must be running it in a loop somewhere to remove the elements.

colours[0].remove('1') is good.

c = [['1', '2', '3'], ['1', '2', '3'], ['1', '2', '3']] c[0].remove('1') print (c) [['2', '3'], ['1', '2', '3'], ['1', '2', '3']] --output

PraveenB
  • 1,270
  • 10
  • 11
  • 3
    As stated in the comments under the question, the problem probably lies in the way the list is created. Your answer doesn't say more than "your code should work" and doesn't solve the problem... – Thierry Lathuille Mar 03 '22 at 14:56
0

That sounds strange to me, I ran a quick test:

colours = [['1', '2', '3'], ['1', '2', '3'], ['1', '2', '3']]
colours[0].remove('1')
print(colours)   #[['2', '3'], ['1', '2', '3'], ['1', '2', '3']]

Update your question with more information if the issue persists

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ariya
  • 1
0

As per your code in the comment:

colours = []
list = ['1','2','3']
for i in range(3):
    colours.append(list)

This is adding the same list 3 times. Therefore when you use:

colours[0].remove('1')

You are basically removing 1 from the original list. Therefore it is applied across colours as well. This is because all the lists in colours point to the same location in memory.

My suggestion is, first don't use the variable name list as it shadows the builtin list. Second to build the list you intended, use different lists every iteration:

colours = []
for i in range(3):
    colours.append(['1','2','3'])

Or using a comprehension:

colours = [['1','2','3'] for i in range(3)]
Jab
  • 26,853
  • 21
  • 75
  • 114