I have encountered a problem while using sort function of python.
>>>d = [23,45,67,5,4,23,45,89]
>>>d[5]
23
>>>g = d
>>>g.sort()
>>>d[5]
45
Is there any way, i can get g sorted without disturbing d.
list.sort sorts in-place, and g = d assigns the reference to the list without copying it. Copy the list as g = d[:] before sorting, or use the built-in function sorted instead:
g = sorted(d)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With