Is it safe to list the list in the source list?

Is the following safe?

x = [1, 2, 3, 4]
x = [y+5 for y in x]

Does the list comprehension first by creating a new list, and then assigns this new list x? I was once told that changing the list upon repeated execution is unsafe.

+5
source share
2 answers

You do not change the list while iterating over it, you create a completely new list, and then, once it has been evaluated, you bind it to the name xso that everything is safe.

+5
source

Yes, it is safe.

, , ( ) x. , , , .

+3

All Articles