Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort() and reverse() functions do not work

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())
like image 894
Amr Bakri Avatar asked May 09 '13 11:05

Amr Bakri


People also ask

How do you reverse sort a function?

Example 2: Sort in descending order The sorted() function accepts a reverse parameter as an optional argument. Setting reverse = True sorts the iterable in the descending order.

Can you reverse sort Python?

Sort in Descending orderThe sort() method accepts a reverse parameter as an optional argument. Setting reverse = True sorts the list in the descending order.

Does sorted return a new list?

Note: The simplest difference between sort() and sorted() is: sort() doesn't return any value while, sorted() returns an iterable list. So in your case answer = sorted(newList) .


3 Answers

.sort() and .reverse() change the list in place and return None See the mutable sequence documentation:

The sort() and reverse() methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don’t return the sorted or reversed list.

Do this instead:

a.sort()
print(a)
a.reverse()
print(a)

or use the sorted() and reversed() functions.

print(sorted(a))               # just sorted
print(list(reversed(a)))       # just reversed
print(a[::-1])                 # reversing by using a negative slice step
print(sorted(a, reverse=True)) # sorted *and* reversed

These methods return a new list and leave the original input list untouched.

Demo, in-place sorting and reversing:

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> a.sort()
>>> print(a)
[1, 66.25, 333, 333, 1234.5]
>>> a.reverse()
>>> print(a)
[1234.5, 333, 333, 66.25, 1]

And creating new sorted and reversed lists:

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(sorted(a))
[1, 66.25, 333, 333, 1234.5]
>>> print(list(reversed(a)))
[1234.5, 1, 333, 333, 66.25]
>>> print(a[::-1])
[1234.5, 1, 333, 333, 66.25]
>>> print(sorted(a, reverse=True))
[1234.5, 333, 333, 66.25, 1]
>>> a  # input list is untouched
[66.25, 333, 333, 1, 1234.5]
like image 101
Martijn Pieters Avatar answered Oct 16 '22 21:10

Martijn Pieters


A simple ascending sort is very easy, call the sorted() function. It returns a new sorted list:

>>> sorted([66.25, 333, 333, 1, 1234.5])
[1, 66.25, 333, 333, 1234.5]

sorted() accept a reverse parameter with a boolean value.

>>> sorted([66.25, 333, 333, 1, 1234.5], reverse=True)
[1234.5, 333, 333, 66.25, 1]
like image 30
Haresh Shyara Avatar answered Oct 16 '22 21:10

Haresh Shyara


For reference, you can see the documentation here specifically says:

The sort() and reverse() methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don’t return the sorted or reversed list.

Don't be afraid to read the manual!

like image 1
Useless Avatar answered Oct 16 '22 20:10

Useless