Si68
(Simon Lock)
August 10, 2021, 10:57am
1
Can anyone help me use sorted() to print a list in reverse alphabetical order please?
Here is my list:
places = [‘Iceland’, ‘Japan’, ‘Manchester’, ‘Norwich’]
As I understand it, the sorted() function can also accept a reverse=True argument to display the list in reverse alpha order, but I am having trouble getting the program to do that
Many thanks in advance
Simon
frostming
(Frost Ming)
August 10, 2021, 11:02am
2
Yes exactly:
In [1]: sorted(places, reverse=True)
Out[1]: ['Norwich', 'Manchester', 'Japan', 'Iceland']
Hi Simon,
If you are having trouble with something, you should show us what you
tried and what result you get. For example:
places = ['Iceland', 'Japan', 'Manchester', 'Norwich']
sorted(places, reversed=True)
gives a traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'reversed' is an invalid keyword argument for sort()
But if I spell it correctly, it works:
sorted(places, reverse=True)
# --> returns ['Norwich', 'Manchester', 'Japan', 'Iceland']
Note that sorted() creates a new list, a copy of the original. If you
want to sort the list in-place without making a copy, use the sort
method like this:
places.sort(reverse=True)
print(places)
Since your list is already sorted, if you just want to reverse it, you
can do that:
places.reverse()
print(places)
Si68
(Simon Lock)
August 10, 2021, 1:47pm
4
Thanks for replying Steven. This is the code:
places = [‘Belgium’, ‘Iceland’, ‘Norwich’, ‘Ulan Bator’]
sorted(places, reverse=True)
print(places)
I am expecting the program to return
[‘Ulan Bator’, ‘Norwich’, ‘Iceland’, ‘Belgium’]
Instead it is just printing the original list in the original order:
[‘Belgium’, ‘Iceland’, ‘Norwich’, ‘Ulan Bator’]
[Finished in 1.0s]
sorted returns a new list . You do not save the result of sorted(places, reverse=True)
in a variable.
https://docs.python.org/3/howto/sorting.html
Si68
(Simon Lock)
August 10, 2021, 2:10pm
7
Thanks for replying
Can you see what I’ve done wrong here?
Please don’t post screenshots of code; use Markdown instead.
Both @steven.daprano and I have explained how sorted()
works. I suggest you read our replies once more and use that information to figure out the missing pieces
If you want to sort a list in place, without creating a new list, you
need the sort method:
mylist = [3, 5, 2, 4, 1]
mylist.sort()
print(mylist) # --> prints [1, 2, 3, 4, 5]
If you use the sorted function, it makes a copy, which you need to store
in a variable"
mylist = [3, 5, 2, 4, 1]
newlist = sorted(mylist)
print(mylist) # --> prints the original [3, 5, 2, 4, 1]
print(newlist) # --> prints [1, 2, 3, 4, 5]
cloaks
(Cloaks)
September 6, 2022, 1:59pm
10
I had trouble with this as well, but if you want to print a temporary list in reverse order, here’s what I had to do:
places= [‘Chile’,‘Japan’,‘Hollywood’, ‘Mexico’, ‘Las Vegas’]
print(sorted(places, reverse=True))
and if you check with print(places), you’ll see the original order of the list has not been changed.
hope this helps