Python Question

Hi All
I don’understan because in the line 2 of attached script return None.
It’s bug or a norma result?
Ciao Vittorio

Hey Vittorio,

It’s normal… list.sort() sorts the list in place - this original list is changed, it doesn’t make a copy and it doesn’t return the sorted list because it’s unnecessary. That’s why print a already shows the sorted list.

If you want a sorted copy and retain the original, use the function sorted():

a=[3,4,5,2,1,6]
b=sorted(a)
print a, b

Returns: [3, 4, 5, 2, 1, 6] [1, 2, 3, 4, 5, 6]

Ciao,
–Mitch

1 Like

Mille Grazie for your explanation.
Ciao
Vittorio