0

When I do

>>> a = [1]*3
>>> a
 [1,1,1]
>>> a[0] = a[0]+1
>>> a
 [2,1,1]

which is perfectly fine, but when I do the same with sets iget the following

>>> a = [set()]*3
>>> a
[set([]), set([]), set([])]
>>> a[0].add(1)
>>> a
 [set([1]), set([1]), set([1])]

Which is a very strange behaviour, any explanation for this?

m.awad
  • 187
  • 2
  • 13

1 Answers1

0

The most logical explanation for this behaviour is that it sees all 3 sets() as a pointer to 1 set, have you tried declaring the sets before adding them to the list and then mutating them?

Sinshz
  • 13
  • 6