You are currently viewing Python Sort List of Lists

How to sort a list of lists in python? Sorting a list of lists in Python is a powerful feature that allows you to sort the inner lists based on a specific element, known as the sorting key. This can be useful in many scenarios, such as when you want to sort a list of records based on a specific field or attribute.

Advertisements

You can sort a list of lists in Python using the sorted() function. By default, sorted() sorts the elements of a list in ascending order. To sort a list of lists based on a specific element in each sublist, you can pass a custom sorting function to the key argument. In this article, I will explain how to sort a list of lists in Python by using the sort() method, and sorted() function with examples.

1. Quick Examples of Sort List of Lists

If you are in a hurry, below are some quick examples of how to sort a list of lists.


# Quick examples of sort list of lists

# Example 1: Use sorted() function to sort a list of lists
lists = [[93, 6], [72, 9], [35, 2]]
print("Sorted Lists based on index 0: % s" % (sorted(lists, key=itemgetter(0))))
lists = [[2500, 'Spark'], [2200, 'Hadoop'], [3000, 'Python']]
print("Sorted Lists based on index 1: % s" % (sorted(lists, key=itemgetter(1))))

# Example 2: use the itemgetter() function
# sort the list of lists based on multiple elements
lists = [[93, 6], [72, 9], [35, 2]]
sorted_list = sorted(lists, key=itemgetter(0, 1))

# Example 3: use the itemgetter() function
# sort a list of lists in descending order
lists = [[1000, 'Java'], [2500, 'Spark'], [2200, 'Hadoop'], [3000, 'Python']]
sorted_list = sorted(lists, key=itemgetter(1), reverse=True)

# Example 4: Sort a list of lists Using lambda expression 
# along with the sorted()
lists = [[1000, 'Java'], [2500, 'Spark'], [2200, 'Hadoop'], [3000, 'Python']]
sorted_list = sorted(lists, key=lambda x:x[0])

# Example 5: Sort a list of lists using lambda 
# along with sorted() method
sorted_list = sorted(lists, key=lambda x:x[1])

# Example 6: Sorted list of lists in descending
sorted_list = sorted(lists, key=lambda x: x[1], reverse=True)

# Example 7: Sort a list of lists 
# using sort() function
lists = [[93, 6], [72, 9], [35, 2]]
lists.sort()

# Example 8: Sort list of lists 
# in descending order
lists.sort(reverse=True)

# Example 9: Sort list of lists in key=len
lists = [[3, 4, 5], [1, 2], [6, 7, 8, 9]]
lists.sort(key=len)

2. Sort a List of Lists Using itemgetter() Function

You can use the itemgetter() function from the operator module as a key function when sorting a list of lists in Python. The itemgetter() function returns a callable object that accesses the specified item(s) of an object, which can be used to sort a list of lists.

For example, the itemgetter() function along with the sorted() function sorts a list of lists based on the first element of each sub-list.


# Import
from operator import itemgetter

# Using sorted() function
# Use itemgetter() function to select the position in the list to sort
lists = [[93, 6], [72, 9], [35, 2]]
print("Sorted Lists based on index 0: % s" % (sorted(lists, key=itemgetter(0))))

# Sort by second element of the inner list
lists = [[2500, 'Spark'], [2200, 'Hadoop'], [3000, 'Python']]
print("Sorted Lists based on index 1: % s" % (sorted(lists, key=itemgetter(1))))

# Output:
# Sorted Lists based on index 0: [[35, 2], [72, 9], [93, 6]]
# Sorted Lists based on index 1: [[2200, 'Hadoop'], [3000, 'Python'], [2500, 'Spark']]

You can also use the itemgetter() function to sort the list of lists based on multiple elements. For example, if you want to sort the list based on the second element of each sub-list in case of a tie with the first element, you can modify the key function.


# Sort the list of lists based on multiple elements
lists = [[93, 6], [72, 9], [35, 2]]
sorted_list = sorted(lists, key=itemgetter(0, 1))
print(sorted_list)

# Output:
# [[35, 2], [72, 9], [93, 6]]

To sort a list of lists in descending order, you can pass the reverse=True argument to the sorted() function. For example, this tells the sorted() function to sort the list in reverse order.


# Sort a list of lists in descending order
lists = [[1000, 'Java'], [2500, 'Spark'], [2200, 'Hadoop'], [3000, 'Python']]
sorted_list = sorted(lists, key=itemgetter(1), reverse=True)
print(sorted_list)

# Output:
# [[2500, 'Spark'], [3000, 'Python'], [1000, 'Java'], [2200, 'Hadoop']]

3. Using lambda along with sorted()

You can also sort a list of lists using lambda expressions along with the sorted() method. To sort by the first element using the lambda expression x[0] for key argument. For example, the key argument is a lambda function that returns the first element of each sublist, which is used as the sorting key. The sorted() function returns a new list with the elements sorted in ascending order based on the sorting key.


# Sort a list of lists Using lambda expression along with the sorted()
lists = [[1000, 'Java'], [2500, 'Spark'], [2200, 'Hadoop'], [3000, 'Python']]
sorted_list = sorted(lists, key=lambda x:x[0])
print(sorted_list)

# Output:
# [[1000, 'Java'], [2200, 'Hadoop'], [2500, 'Spark'], [3000, 'Python']]

# Sort a list of lists using lambda along with sorted() method
sorted_list = sorted(lists, key=lambda x:x[1])
print(sorted_list)

# Output:
# [[2200, 'Hadoop'], [1000, 'Java'], [3000, 'Python'], [2500, 'Spark']]

You can sort a list of lists in descending order by the second element by using the reverse parameter of the sorted() function to specify the index to sort by using the key parameter. For example, the reverse parameter is set to True to sort the list in descending order, and the key parameter is set to lambda x:x[1] to sort by the second element of each list (index 1).


# Sorted list of lists in descending
sorted_list = sorted(lists, key=lambda x: x[1], reverse=True)
print(sorted_list)

# Output
# [[2500, 'Spark'], [3000, 'Python'], [1000, 'Java'], [2200, 'Hadoop']]

4. Sort a List of Lists Using sort() Method

Alternatively, to sort a list of lists in ascending order, you can also use the sort() method on the list object. By default, the sort() method sorts the elements of a list in ascending order.


# Sort a list of lists using sort() function
lists = [[93, 6], [72, 9], [35, 2]]
lists.sort()
print(lists)

# Output:
# [[35, 2], [72, 9], [93, 6]]

To sort in descending order, use the sort() method with the reverse argument set its value to reverse=True.


# Sort list of lists in descending order
lists.sort(reverse=True)
print(lists)

# Output
# [[93, 6], [72, 9], [35, 2]]

The key argument in the sort() method can be used to specify a custom sorting key, in this case the length of the inner lists. The len function is used as the key argument to sort the list of lists according to the length of the inner lists.


# Sort list of lists in key=len
lists = [[3, 4, 5], [1, 2], [6, 7, 8, 9]]
lists.sort(key=len)
print(lists)

# Output:
# [[1, 2], [3, 4, 5], [6, 7, 8, 9]]

Conclusion

In this article, I have explained how to sort the list of lists in python by using itemgetter(), sort(), and built-in sorted() function with examples. By using these you can sort the list of lists in ascending order. To sort in descending order, use reverse=True argument.

Happy Learning !!

Related Articles

References