• Register
0 votes
154 views

Problem :

I am unable to understand what am I missing here:

Sort my_short_names in reverse alphabetic order. The sample output from the given program as below :

['Tody', 'Samy', 'Joey', 'Jany', 'Anny']

Below is my code for your reference :

my_short_names = ['Jany', 'Samy', 'Anny', 'Joey', 'Tody']
my_short_names.sort()
print(my_short_names)

 

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution  :

I had also faced the similar issue in the recent past. I did lot of research on it and found the solution on it. This is the very common problem with the people getting started.

As I always say, first please have the very careful look at the documentation for list.sort:

sort(*, key=None, reverse=None)

 This method sorts your list in a place, using only the < comparisons between items.

The reverse is the boolean value. If it is set to True, then your list elements are sorted as if each comparison were to be reversed.

So the items in the list will be sorted from the "smallest" to the "largest" using your < comparion operator, which for strings clearly means the lexicographical ordering (A < AB < B). So to start the sort in reverse order, you need to use the reverse parameter as shown below :

my_short_names.sort(reverse=True)

For more information on it please have a look at the official information Sorting HOW TO.

 

38.6k points

Related questions

1 vote
1 answer 61 views
61 views
Problem: Any ideas on what I need to do to fix this? sort short_names in reverse alphabetic order. sample output from given program:
asked Mar 18 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 13 views
13 views
Problem: How to download folder from google drive? Please assist me.
asked Jun 4 Rada Gracelynn 100k points
0 votes
0 answers 6 views
6 views
Problem: Is there somebody who can assist : Winerror 2 the system cannot find the file specified python?
asked Jun 23 Nahil 122k points
0 votes
1 answer 23 views
23 views
Problem: Is anyone available to assist me and resolve this issue? Python script to read a file line by line
asked May 5 Ifra 43.4k points
0 votes
1 answer 29 views
29 views
Problem: I installed the latest version of Python (3.6.4 64-bit) and the latest version of PyCharm (2017.3.3 64-bit). Then I installed some modules in PyCharm (Numpy, Pandas, etc), but when I tried installing Tensorflow it didn't install, and I ... some of the things which were suggested to other people, but nothing worked (this included installing Flask). How can I install Tensorflow? Thanks.
asked Apr 25 sumaiya simi 43.9k points
0 votes
1 answer 24 views
24 views
Problem: How to add 50 to the number stored under the 'gold' key?
asked Apr 12 ummesalma 29.1k points
0 votes
1 answer 5 views
5 views
Problem: Global name 'filenotfounderror' is not defined
asked Jul 5 Shifa 18.2k points
0 votes
1 answer 9 views
9 views
Problem: Can anyone assist me with this issue : View must be a callable or a list/tuple in the case of include()?
asked Jun 20 Florina Gulnar 103k points
0 votes
1 answer 77 views
77 views
Problem: I am facing following error: post data should be bytes, an iterable of bytes, or a file object. it cannot be of type str.
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 2.3k views
2.3k views
Problem : I am trying to convert my working Python 2.7 code into Python 3 code but I am facing following error post data should be bytes, an iterable of bytes, or a file object. it cannot be of type str.
asked Nov 16, 2019 peterlaw 6.9k points