• Register
0 votes

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']


7.5k points

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 :


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


38.6k points

Related questions

0 votes
1 answer 707 views
Problem : I learning Python with the help of free online Python tutorial which wants me to write following Program: Create a temperature converter which will convert Fahrenheit values to Celsius and vice-versa using the following two formulas which relate the temperature f in Fahrenheit to ... ,sep ='') if inputString[-1] == 'F': farenheit = inputDigit print(celsiusConvert(farenheit),'C', sep='')
asked Nov 21, 2019 peterlaw 6.9k points
0 votes
1 answer 1.2K 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
0 votes
1 answer 323 views
Problem: I have recently started learning Python so I have only fundamental knowledge in Python. Please find below my Transaction class for your reference: class Transaction(object):     def __init__(self, company, num, price, date, is_buy):         self.company = company         ... Python and because of lockdown I am unable to find any help to fix this issue. So I am posting my question here.
asked Aug 10 Raphael Pacheco 4.9k points
1 vote
1 answer 134 views
Problem: I am getting an interesting error: valueerror: mixing iteration and read methods would lose data while I&rsquo;m trying to run my script on python. I have no idea about this problem. I am trying to open a text file in my program and read it. Please ... test_file I think I am doing something wrong with using the readline() function. I am looking forward to having a solution. Thanks, guys.
asked Mar 29 Gavin 15.3k points
0 votes
1 answer 306 views
Problem : I am trying to execute below commands. import numpy as np import cv But I am getting following error on my console: >>> runfile('/Users/isaiahnields/.spyder2/temp.py', wdir='/Users/isaiahnields/.spyder2') RuntimeError: module compiled against API version a but this ... it is not working. I had added cv.py to the python2.7 folder in Spyder-Py2 is there anything else I need to add?
asked Dec 4, 2019 alecxe 7.5k points