reverse
: True or False , by default reverse=Falsekey
: the function to use for sorting
my_list=['Ronald','John','Alex','Ravi']
my_list.sort()
print(my_list)
Output
['Alex', 'John', 'Ravi', 'Ronald']
my_list=[4,2,7,1]
my_list.sort() # by default ascending
print(my_list)
Output is here
[1, 2, 4, 7]
reverse=True
my_list=[4,2,7,1]
my_list.sort(reverse=True) # Descending order
print(my_list) # Output [7, 4, 2, 1]
my_list=['ab','xa','dc','Cxx','yzza','Z','b']
my_list.sort(key=len)
print(my_list)
Output
['Z', 'b', 'ab', 'xa', 'dc', 'Cxx', 'yzza']
my_list=['ab','xa','dc','Cxx','yzza','Z','b']
my_list.sort(key=len,reverse=True)
print(my_list)
Output
['yzza', 'Cxx', 'ab', 'xa', 'dc', 'Z', 'b']
my_list=[4,2,7,1]
my_list=my_list.sort()
print(my_list) # None
All list methods
Questions with solutions on ListAuthor
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.