The Python Sort List of Tuples

In Python, we utilize tuples to store multiple elements in a variable. Tuple lists may be sorted similarly to any other list. However, tuples contain numerous elements, so we are free to rearrange the tuples based on item 1 or item i. In this guide, we will look at how to sort various lists of tuples using multiple examples. We will go over the various scenarios and the many strategies employed to accomplish this. We will mainly focus on some built-in methods and sorting procedures to achieve tuple sorting. The well-organized nature and invariant properties of tuples are dissimilar from those in regular lists.

Utilize Inbuilt List.sort() Function to Arrange the List of Tuples

The sort() method categorize the items of a list in both increasing and decreasing sequences. The key parameter indicates the value used when sorting. The key is assumed to be a method or further callable object that can be functional to any list of items.

In this code, we utilize the built-in sort() technique to sort the list of tuples, and then, we get a fully sorted tuplet in ascending order centered on different ages of the mentioned friends. We may also sort the tuples in descending sequence by slightly modifying this code.

To implement the codes in Python, we have to install the Spyder5 version. We created a new project named “”. To create a new file, we pressed “Ctrl+N” from the keyboard.s:

The Sorted() technique sorts any list and returns the sorted list of items without changing the original order. It takes three arguments; two of them are optional. Here, “friends” is a parameter that needs to be organized. The key is optional. A method that serves as the key or origin for sort assessments. The reverse is another argument. The key has been specified to arrange to use the second item of the lambda sublist. Here, 1 signifies the 1st item of the list. To sort the list in ascending sequence, we can ignore the third argument that we have created in this program. If it is set to true, iterable will be arranged in a reverse (descending) sequence. By default, it is set to false:

The print statement prints the sorted list of “friends” (list).

Utilize Bubble Sort to Arrange Tuples List

Sorting is possible with the bubble sort technique. Every tuple is an item of the specified list. We utilize a nested loop to get the second item of every tuple. This implements the in-place sorting technique.

Let’s look at the friends’ problem, and we solve this example by using the bubble sort technique. Here, we want to accumulate the items according to the 0th stint of the tuple:

We write in Python code to sort a tuple list. First, we define a function for sorting the list of friends’ names by its second element. Now, we get the length of a list of friends’ names. We use a nested loop here to sort the list:

In this case, we utilize the bubble sort technique to gather the tuples centered on the friend 0th component. We notice that we write considerably longer codes than some built-in approaches. It is not deliberate as the finest way for the memory. However, it is one of the simple and easy methods to sort the list of tuples.

Sort List of Tuples by Using Sorted() function

When we do sorting using this technique, the real data or information of the tuple changes, and the in-place sorting process is performed. The sorted() method sorts the specified input in a particular direction and returns the sorted repeatability in the form of a list. Let’s consider the example of this approach:

In this instance, we do sorting by the use of the sorted() technique by taking two different instances using the sorted() function. In the first case, we take a variable and add several elements. After this, we will acquire an array settled in ascending order. The key has been specified to arrange using the second item of the lambda sublist. Here, 1 denotes the 1st item of the tuple.

In the second instance, we looked at the numbers for the beyond method. Again, we acquire an array set in ascending order according to the various friends’ names:

Both lists are sorted in ascending order.


This article discussed tuples for Python lists. We discussed several approaches that are used for sorting the lists of a tuple. We sorted the lists by bubble sort and sorted() function. By this article, we understand that pairs can be sorted in the same way as regular lists. We hope you found this article helpful. Check out more Linux Hint articles for tips and tutorials.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content