Python Forum
Python sorts all arrays instead of one. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python sorts all arrays instead of one. (/thread-20843.html)



Python sorts all arrays instead of one. - Alex009988 - Sep-02-2019

Hello. I want only to sort c1. Using that code it sorts both c and c1
def f(v):
    x=v[0]
    y=v[1]
    return x**2 + x*y + y**2 - 6*x - 9*y
v1 = [0, 0]
v2 = [1, 0]
v3 = [0, 1]
v=[v1,v2,v3]
c=[f(v1),f(v2),f(v3)]
c1=c
c1.sort()
print(*c)
print(*c1)
How to get sorted only c1?


RE: Python sorts all arrays instead of one. - buran - Sep-02-2019

https://python-forum.io/Thread-List-modification-returns-none?pid=90218#pid90218


RE: Python sorts all arrays instead of one. - DeaD_EyE - Sep-02-2019

Make a copy of the list. The inline sort mutates the list.
It's like appending, removing or inserting items.

The names c and c1 referring to the same object.

If you don't want to mutate the list, you can use the built-in function sorted(). This does not mutate the object, sorted creates a new list, which is sorted.