Sorting a list in Python with Example

In this article, we learn how you can sort a python list and how sorting a list in python works with some examples. Also, understand the different sorting techniques used in python to sort the list items.

Sorting a list in Python

 

Sorting a list in Python

To sort a list in python a sort() method is used. It directly modifies the original list and sorts the order of items. You can sort the items in both ascending and descending order.

 

Sort list in ascending order in Python

# create an unsorted list
numbers = [5, 3, 8, 1, 9, 6]

# sort the list in ascending order (default)
numbers.sort()
print(numbers)

Output-

# [1, 3, 5, 6, 8, 9]

 

As I said earlier sort() method doesn’t return anything because it modifies the original list. You can also sort the list in descending order bypassing the “reverse=True” argument to the sort() method. This is an optional parameter by default it’s ascending.

 

Sort list in descending order in Python


# sort the list in descending order

numbers = [5, 3, 8, 1, 9, 6]

numbers.sort(reverse=True)

print(numbers)

Output-

# [9, 8, 6, 5, 3, 1]

 

This is how you can use the sort() method to sort the list in python but Alternatively, you can use the sorted() function to sort a list and return a new sorted list, without modifying the original list:

Sort list using the sorted() method Python


numbers = [5, 3, 8, 1, 9, 6]

# sort the list in ascending order (default) and return the new list

sorted_numbers = sorted(numbers)

print(sorted_numbers)

Output-

# [1, 3, 5, 6, 8, 9]

 

Sort a list of JSON objects in Python

In order to sort a list of JSON objects in Python, you can use the sorted() method and pass a key function as the key argument. The key function is used to extract a value from each JSON object that will be used for sorting.

 

Example –


import json

# sample JSON list
data = '[{"name": "Mike", "age": 30}, {"name": "Barb", "age": 25}, {"name": "Tomy", "age": 35}]'

# parse JSON string into a list of dictionaries
data = json.loads(data)

# sort the list by the "age" key in ascending order
sorted_data = sorted(data, key=lambda x: x["age"])
print(sorted_data)

Output-

#[{"name": "Barb", "age": 25}, {"name": "Mike", "age": 30}, {"name": "Tomy", "age": 35}]

 

Here, the sorted() function is used to sort the list of JSON objects by “age”. The key argument is set to a lambda function that takes each JSON object (x) and returns its “age” value.

 

So which one should we use let’s know the key difference between the sort() and sorted() methods.

 

Difference between sort() and sorted() in python

 

The sort() method and the sorted() function in Python are both used to sort lists, but they work in slightly different ways. Below are some key differences –

 

  1. The sort() method modifies the original list in place and does not return a new list, whereas the sorted() function returns a new list that is sorted, without modifying the original list.
  2. The sort() method is generally faster than the sorted() function because it modifies the original list in place, whereas the sorted() function creates a new list.
  3. The sort() method is called on a list and has no return value, whereas the sorted() function is a built-in function and is called with the list as an argument.

 

Conclusion

This is how you can sort the list in python and you also learn the differences between sort and sorted methods.