Why output like this?
years = [1998,2000,1998,1987]
x = years
x.sort()
print(x)
print(years) #why not [1998,2000,1998,1987]
Output:
[1987, 1998, 1998, 2000]
[1987, 1998, 1998, 2000]
Why output like this?
years = [1998,2000,1998,1987]
x = years
x.sort()
print(x)
print(years) #why not [1998,2000,1998,1987]
Output:
[1987, 1998, 1998, 2000]
[1987, 1998, 1998, 2000]
When you assign it to another variable the reference to that list is assign to another variable, so both of them assigning to same list, then when you change the list, both of them will be change.
you can use copy
to prevent this to happen, like this:
from copy import copy
x = copy(years)
or
x = years[:]