Python Sort List – How to Order By Descending or Ascending (2025)

/ #Python
Python Sort List – How to Order By Descending or Ascending (1)
Jessica Wilkins
Python Sort List – How to Order By Descending or Ascending (2)

In Python, you can sort data by using the sorted() method or sort() method.

In this article, I will provide code examples for the sorted() and sort() methods and explain the differences between the two.

What is the sort() method in Python?

This method takes a list and sorts it in place. This method does not have a return value.

In this example, we have a list of numbers and we can use the sort() method to sort the list in ascending order.

my_list = [67, 2, 999, 1, 15]# this prints the unordered listprint("Unordered list: ", my_list)# sorts the list in placemy_list.sort()# this prints the ordered listprint("Ordered list: ", my_list)

Python Sort List – How to Order By Descending or Ascending (3)

If the list is already sorted then it will return None in the console.

my_list = [6, 7, 8, 9, 10]# this will return None because the list is already sortedprint(my_list.sort())

Python Sort List – How to Order By Descending or Ascending (4)

The sort() method can take in two optional arguments called key and reverse.

key has the value of a function that will be called on each item in the list.

In this example, we can use the len() function as the value for the key argument. key=len will tell the computer to sort the list of names by length from smallest to largest.

names = ["Jessica", "Ben", "Carl", "Jackie", "Wendy"]print("Unsorted: ", names)names.sort(key=len)print("Sorted: ", names)

Python Sort List – How to Order By Descending or Ascending (5)

reverse has a boolean value of True or False.

In this example, reverse=True will tell the computer to sort the list in reverse alphabetical order.

names = ["Jessica", "Ben", "Carl", "Jackie", "Wendy"]print("Unsorted: ", names)names.sort(reverse=True)print("Sorted: ", names)

Python Sort List – How to Order By Descending or Ascending (6)

How to use the sorted() method in Python

This method will return a new sorted list from an iterable. Examples of iterables would be lists, strings, and tuples.

One key difference between sort() and sorted() is that sorted() will return a new list while sort() sorts the list in place.

In this example, we have a list of numbers that will be sorted in ascending order.

sorted_numbers = sorted([77, 22, 9, -6, 4000])print("Sorted in ascending order: ", sorted_numbers)

Python Sort List – How to Order By Descending or Ascending (7)

The sorted() method also takes in the optional key and reverse arguments.

In this example, we have a list of numbers sorted in descending order. reverse=True tells the computer to reverse the list from largest to smallest.

sorted_numbers = sorted([77, 22, 9, -6, 4000], reverse=True)print("Sorted in descending order: ", sorted_numbers)

Python Sort List – How to Order By Descending or Ascending (8)

Another key difference between sorted() and sort() is that the sorted() method accepts any iterable whereas the sort() method only works with lists.

In this example, we have a string broken up into individual words using the split() method. Then we use sorted() to sort the words by length from smallest to largest.

my_sentence = "Jessica found a dollar on the ground"print("Original sentence: ", my_sentence)print(sorted(my_sentence.split(), key=len))

Python Sort List – How to Order By Descending or Ascending (9)

We can also modify this example and include the key and reverse arguments.

This modified example will now sort the list from largest to smallest.

my_sentence = "Jessica found a dollar on the ground"print("Original sentence: ", my_sentence)print(sorted(my_sentence.split(), key=len, reverse=True))

Python Sort List – How to Order By Descending or Ascending (10)

We can also use the sorted() method on tuples.

In this example, we have a collection of tuples that represents the band student's name, age and instrument.

band_students = [ ('Danny', 17, 'Trombone'), ('Mary', 14, 'Flute'), ('Josh', 15, 'Percussion')]

We can use the sorted() method to sort this data by the student's age. The key has the value of a lambda function which tells the computer to sort by age in ascending order.

A lambda function is an anonymous function without a name. You can define this type of function by using the lambda keyword.

lambda student: student[1]

To access a value in a tuple, you use bracket notation and the index number you want to access. Since we start counting at zero, the age value would be [1].

Here is the complete example.

band_students = [ ('Danny', 17, 'Trombone'), ('Mary', 14, 'Flute'), ('Josh', 15, 'Percussion')]print(sorted(band_students, key=lambda student: student[1]))

Python Sort List – How to Order By Descending or Ascending (11)

We can modify this example and sort the data by instrument instead. We can use reverse to sort the instruments through reverse alphabetical order.

band_students = [ ('Danny', 17, 'Trombone'), ('Mary', 14, 'Flute'), ('Josh', 15, 'Percussion')]print(sorted(band_students, key=lambda student: student[2], reverse=True))

Python Sort List – How to Order By Descending or Ascending (12)

Conclusion

In this article, we learned how to work with Python's sort() and sorted() methods.

The sort() method only works with lists and sorts the list in place. It does not have a return value.

The sorted() method works with any iterable and returns a new sorted list. Examples of iterables would be lists, strings, and tuples.

Both of these methods have two optional arguments of key and reverse.

key has the value of a function that will be called on each item in the list.

reverse has a boolean value of True or False.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Python Sort List – How to Order By Descending or Ascending (13)
Jessica Wilkins

Read more posts.

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTISEMENT

Python Sort List – How to Order By Descending or Ascending (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Dan Stracke

Last Updated:

Views: 6228

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.