0
import collections from deque


test = [deque()] * 3
test[2].append(7)
print(test)

I am expecting the above to print: [deque([]), deque([]), deque([7])]

but instead i get: [deque([7]), deque([7]), deque([7])]

What is the reason for this?

  • Never use `*` with lists. Stick with `[deque() for _ in range(3)]`, and you'll never have this problem. – chepner Jun 22 '22 at 20:47
  • Thank you for your help, I searched and found there was a similar question in following link [link](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – iechodropz Jun 22 '22 at 20:51

1 Answers1

0

Your list contains three times the same deque not three different deques.

ljmc
  • 4,830
  • 2
  • 7
  • 26