I was trying to test how the lists in python works according to a tutorial I was reading.
When I tried to use list.sort()
or list.reverse()
, the interpreter gives me None
.
Please let me know how I can get a result from these two methods:
a = [66.25, 333, 333, 1, 1234.5]
print(a.sort())
print(a.reverse())
This methods operate in place.
This code works (python 3.x)
A simple ascending sort is very easy, call the sorted() function. It returns a new sorted list:
sorted() accept a reverse parameter with a boolean value.
.sort()
and.reverse()
change the list in place and returnNone
See the mutable sequence documentation:Do this instead:
or use the
sorted()
andreversed()
functions.These methods return a new list and leave the original input list untouched.
Demo, in-place sorting and reversing:
And creating new sorted and reversed lists:
For reference, you can see the documentation here specifically says:
Don't be afraid to read the manual!