Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
108 views
in Python by (176k points)
How do you sort a list in reverse order in Python?

Please log in or register to answer this question.

1 Answer

0 votes
by (176k points)

In Python, you can sort a list in reverse order by specifying the reverse=True argument when calling the sort() method or the sorted() function.

Here is an example of sorting a list in reverse order using the sort() method:

my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
my_list.sort(reverse=True)
print(my_list)
 

Output:

[9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
 

In this example, the sort() method is called with the reverse=True argument, which sorts the list in reverse order.

Alternatively, you can use the sorted() function to sort a list in reverse order:

my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_list = sorted(my_list, reverse=True)
print(sorted_list)
 

Output:

[9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
 

In this example, the sorted() function is called with the reverse=True argument, which returns a new sorted list in reverse order.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 20, 2023 in Python by kvdevika (176k points)
0 votes
1 answer
0 votes
2 answers
asked Mar 20, 2023 in Python by kvdevika (176k points)
+1 vote
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...