Python Forum

Full Version: Python sorts all arrays instead of one.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.