Sort 2D List in Python (2 Examples)

 

Hi! This short tutorial will show you how to rearrange a 2D list in ascending order in the Python programming language.

Here is an overview:

Let’s dive into the Python code!

 

Create Example 2D List of Integers

Here, we will create the example Python 2D list of integers that will be ordered in this tutorial.

Therefore, in your preferred Python IDE, run the code below to create the example 2D list:

my_list = [[3, 4], [1, 2], [5, 6]]
 
print(my_list)
 
# [[3, 4], [1, 2], [5, 6]]
 
print(type(my_list))
 
# <class 'list'>

With the Python 2D list of integers created, we are now going to examine two ways to order the list.
 

Example 1: Order 2D List Using sorted() Function

In this first example, we are going to use the sorted() function to sort the 2D list:

sorted_list = sorted(my_list, key=lambda x: x[0])
 
print(sorted_list) 
 
# [[1, 2], [3, 4], [5, 6]]
 
print(type(sorted_list))
 
# <class 'list'>

Here, the sorted() function returns a sorted list based on the items in the input list. In this method, we parse the input list to the sorted() function, along with a lambda function that specifies the key by which we want to sort the list. The lambda function takes a single argument (a sublist of the 2D list) and returns the value that we want to use as the sorting key, which in this case, is the first element in each sublist.

 

Example 2: Order 2D List Using the sort() Method

In this next example, we will use the sort() method to rearrange the 2D list in ascending order:

my_list.sort(key=lambda x: x[0])
 
print(my_list)
 
# [[1, 2], [3, 4], [5, 6]]
 
print(type(my_list))
 
# <class 'list'>

The sort() method is similar to the sorted() function, but it sorts the list in place (i.e., it modifies the original list rather than creating a new one). In this example, we call the sort() method on the input list and parse a lambda function as the key argument to specify the sorting key.

The lambda function works the same way as in the previous example. For instance, to sort a 2D list based on the first element of each sublist, we defined the lambda function as lambda x: x[0].

Difference Between sorted() Function & sort() Method

The sorted() function and the sort() method are both used to sort elements in a list in Python, but they have some differences in their usage and behavior:

  • Usage: sorted() is a built-in function that takes an iterable as input and returns a new sorted list, leaving the original list unchanged. On the other hand, sort() is a method of the list class that sorts the list in place (i.e., it modifies the original list) and doesn’t return a new list.
  • Return value: sorted() returns a new sorted list, while sort() doesn’t return anything – it sorts the original list in place.
  • Arguments: sorted() takes an iterable as its first argument and can take additional arguments like key and reverse to customize the sorting. sort() is a method of the list class, so it doesn’t take any arguments other than key and reverse.
  • Mutable vs. Immutable: Since sorted() returns a new list, it can be used to sort any iterable, including immutable types like tuples and strings. On the other hand, sort() can only be used on mutable types like lists.

 

So, with that, we have demonstrated with 2 examples how to sort or order a 2D list in Python. I do hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to sort a 2D list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to sort a 2D list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to sort a 2D list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top