Friday, January 29, 2021

 Python : It is possible to Iterate and Add to a list simultaneously but not to a set

>>> for i in a:

...     print(i)

...     if i==10:

...             break

...     a.append(i+1)

... 

1

2

3

4

5

6

7

8

9

10

>>> 

>>> a = {1}

>>> for i in a:

...     print(i)

...     if i==10:

...             break

...     a.add(i+1)

... 

1

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

RuntimeError: Set changed size during iteration

>>>