Unexpected list behavior in Python

The following:

list_reversed = list 

makes the two variables refer to the same list. When you change one, they both change.

To make a copy, use

list_reversed = list[:]

Better still, use the builtin function instead of writing your own:

list_reversed = reversed(list)

P.S. I’d recommend against using list as a variable name, since it shadows the builtin.

Leave a Comment