# Python sort list of tuples

In this Python tutorial, we will learn about the Python sort list of tuples. Also, we will discuss:

• Python sort list of tuples
• Python sort list of tuples by second element
• How to sort list of tuples by first element descending in Python
• How to sort list of tuples in Python by length
• Python sort list of tuples descending
• Python sort list of tuples by last element
• Python sort list of tuples by first and second element
• Python sort list of tuples by multiple elements
• Python sort list of tuples by second value
• Python sort list of tuples alphabetically
• Python sort list of tuples multiple keys
• Python sort list of tuples by key
• Python sort list of tuples by first
• Python program to sort a list of tuples using lambda
• Python sort list of tuples by second value descending

How to sort list of tuples in Python? In Python, we can use the sort() method to sort the elements in the ascending order, and by default, it will sort the first element.

## Python sort list of tuples

• Let us see how to sort a list using tuples in Python.
• In python, we can store multiple values in a single variable by using tuples in Python. Suppose if you have a list of tuples and now you want to sort the tuples in the list.
• There are various methods to solve this task
• By using list.sort() function
• By using bubble sort algorithm
• By using sorted() function

## By using list.sort() function

• Here we can check how to sort the list of tuples by using the list.sort() function in Python.
• This function is a built-in-list method and it sorts the elements of a given list. Sort() function accepts the only key argument that identifies the value and returns the value to be used in the sorting.

Example:

Now, let’s take an example and check how to sort the list of tuples

new_list = [('john', 6), ('Micheal', 9), ('George', 2), ('steve', 19)]

new_list.sort(key=lambda y: y[1])

print(new_list)

In the above code, we have to sort the tuples based on the second element.

Here is the output of the following given code

## By using bubble sort algorithm

• Bubble sort is a simple sorting algorithm that sorts n number of elements in a list. It swaps the adjacent items in a given list if they are in the wrong position order. After that, it repeats the process until all elements are sorted.
• In this example, we will take the list of tuples and sort them by using the bubble sort algorithm.

Example:

new_list = [('Germany',120), ('France',110) , ('England',40) , ('Japan',184), ('China',20)]

new_ele = 0
new_lis_len = len(new_list)
for k in range(0, new_lis_len):
for l in range(0, new_lis_len-k-1):
if (new_list[l][new_ele] > new_list[l + 1][new_ele]):
new_tem = new_list[l]
new_list[l]= new_list[l + 1]
new_list[l + 1]= new_tem

print(new_list)

Note: In the above code, the new_ele variable specifies the index position. In this case, we can use the first element to sort the list of tuples.

Here is the execution of the following given code

## By using sorted() function

• Sorted is a built-in function in Python. This function can be used on all iterables if you want to modify the sorting process then you can use the key function by default its value will be none.
• In this example we have a list of tuples and you can sort this list by using sorted() function.

Example:

new_list = [(2,6,9),(0,9,7),(1,7,6),(9,1,2)]

b = sorted(new_list)
print(b)

Output:

This is how to sort a list of tuples in Python.

## Python sort list of tuples

The sort() method is used to sort the elements in ascending order and by default, it will sort the first element. If two tuples have the same first element then it will sort them according to the second element.

Example:

roll_list1 = [('Jack', 76), ('Beneth', 78), ('Cirus', 77), ('Faiz', 79)]
roll_list1.sort()
print(roll_list1)

After writing the above code (python sort lists of tuples), Ones you will print “roll_list1” then the output will appear as a “ [(‘Beneth’, 78), (‘Cirus’, 77), (‘Faiz’, 79), (‘Jack’, 76)] ”. Here, sort() method is used to sort the elements in ascending order and it will be sorted by its first element. You can refer to the below screenshot for creating python sort lists of tuples.

## Python sort list of tuples by second element

Let us see another example, python sort list of tuples by second element.

In python, if you want to sort a list of tuples by the second element then we have the function called sort() and by using lambda function as a key function. A custom comparator is a key function in sort().

Example:

roll_list1 = [('Jack', 76), ('Beneth', 78), ('Cirus', 77), ('Faiz', 79)]
roll_list1.sort(key=lambda a: a[1])
print(roll_list1)

After writing the above code (python sort list of tuples by the second element), Ones you will print “roll_list1” then the output will appear as a “ [(‘Jack’, 76), (‘Cirus’, 77), (‘Beneth’, 78), (‘Faiz’, 79)] ”.

Here, sort() method is used to sort the elements in ascending order and the list of tuples will be sorted by its second element.

Check the screenshot on python sort list of tuples by second element.

This is how to sort list of tuples by the second element in Python.

## Python sort list of tuples by first element descending

In python, to sort list of tuples by the first element in descending order, we have to use the sort() method with the parameter ” (reverse=True) “ which will sort the elements in descending order.

Example:

roll_list1 = [('Jack', 76), ('Beneth', 78), ('Cirus', 77), ('Faiz', 79)]
roll_list1.sort(reverse=True)
print(roll_list1)

After writing the above code (python sort list of tuples by first element descending), Ones you will print “roll_list1” then the output will appear as a “ [ (‘Jack’, 76), (‘Faiz’, 79), (‘Cirus’, 77), (‘Beneth’, 78)] ”.

Here, sort() method is used with the parameter as “(reverse= True)” to sort the elements in descending order and it will be sorted by its first element.

Check the screenshot.

This is how to sort list of tuples by the first element descending order.

## Python sort list of tuples by length

In python, to sort list of tuples by length, we have the sort() method and the parameter should be pass to get the length of the first element.

Example:

roll_list1 = [('Jack', 76), ('Beneth', 78), ('Cirus', 77), ('sid', 79)]
roll_list1.sort(key=lambda a: len(a[0]))
print(roll_list1)

After writing the above code (python sort list of tuples by length), Ones you will print “roll_list1” then the output will appear as a “ [(‘Sid’, 79), (‘Jack’, 76), (‘Cirus’, 77), (‘Beneth’, 78)] ”.

Here, sort() method is used with the parameter, and the length is calculated and the element is sorted in ascending order by its first element. You can refer to the below screenshot for the python sort list of tuples by length.

The above Python code you can use to sort list of tuples by length.

## Python sort list of tuples descending

• To sort the list of tuples by descending order we can easily use the sort() function in Python.
• First, we will take the list of tuples and arrange them in unsorted order. Now the sort() function will change the order from increasing to decreasing by passing reverse=True as an argument of the sort() method.

Example:

Let’s take an example and check how to sort the list in descending order by using the reverse argument

my_list = [('z',80),('f',90),('g',84),('d',92),('a',96)]

my_list.sort(key=lambda i:i[1],reverse=True)
print(my_list)

Here is the Screenshot of the following given code

This is how to sort list of tuples in descending order in Python.

## Python sort list of tuples by last element

• Let us see how to sort the list of tuples by the last element in Python.
• In the sorted() function the key parameter specifies a custom sort order. In this example, we will take a list of tuples and we want to sort tuples based on the last element of each tuple.

Example:

new_lis=[(4, 2), (7, 8), (3, 1)]
las_ele = sorted(new_lis, key=lambda i: i[-1])
print (las_ele)

Here is the execution of the following given code

This is an example of Python sort list of tuples by last element.

## How to sort list of tuple by last element in Python

Using the bubble sort algorithm, we can easily access the last element of each tuple and use nested loops to perform this particular task.

Here is the Source code

def my_list(las_ele):

end = len(las_ele)
for o in range(0, end):

for p in range(0, end-o-1):
if (las_ele[p][-1] > las_ele[p + 1][-1]):
new_temp = las_ele[p]
las_ele[p]= las_ele[p + 1]
las_ele[p + 1]= new_temp
return las_ele

las_ele =[(9, 6), (8, 3), (4, 1)]

print(my_list(las_ele))

In this example first, we will define a function and pass list as an argument that returns the last element of each tuple.

Here is the output of the following given code

This is how to sort list of tuples by last element in Python.

## Python sort list of tuples by first and second element

• Here we can check how to sort the list of tuples by the first and second elements
• To sort the first tuple element we can use an index() method like list[0]. Similar to the second element we can use the list[1] index.
• By using the sort method we need to sort the lists in place and order.

Example:

my_list = [('b', 4), ('d', 1), ('e', 6), ('m', 2)]

my_list.sort(key=lambda s:s[1])
print("First element values:",my_list)
my_list.sort(key=lambda i:i[0])
print("Second element values:",my_list)

Output:

This is how to sort list of tuples by first and second element in Python.

## Python sort list of tuples by multiple elements

• Here we can check how to sort the list of tuples by including multiple elements.
• Let us assume we have multiple elements in each tuple that we want to sort with the double conditions. In this example, we would first sort the second element of each tuple.
• To do this task we can use the sorted() function to sort the elements and returns a tuple containing the sort order.

Example:

Let’s take an example and check how to sort the list of tuples by multiple elements

my_tup_lis = [(2, 4), (9, 16), (1, 12), (5, 4)]
mul_sort = sorted(my_tup_lis, key=lambda t: (t[1], t[0]))

print("Multiple elements sorted",mul_sort)

Screenshot of the following given code

The above code we can use to sort list of tuples by multiple elements in Python.

## Python sort list of tuples alphabetically

• Let us see how to sort the list of tuples alphabetically in Python.
• By using the sort() method we can easily sort the list of tuples alphabetically.
• To do this task first we have to create a function and pass the list as an argument. In the sort method, the key parameter is set to sort the element by using the lambda function. You can also use key=itemgetter as an argument instead of lambda.

Example:

Here is the Source code

def alpha_tuple(new_list):

new_list.sort(key = lambda a: a[0])
return new_list
new_list = [("b", 41), ("u", 46), ("n", 31),
("a", 81), ("m", 92)]
print("sorted elements alphabetically:",alpha_tuple(new_list))

Here is the execution of the following given code

The above Python code we can use to sort list of tuples alphabetically.

## How to sort list of tuple alphabetically by using sorted() function in Python

Here is another way to sort list of tuples alphabetically by using the sorted() function in Python.

Source Code:

new_list = [(2,'z'),(9,'f'),(4,'c'),(6,'b')]

b = sorted(new_list, key = lambda u: u[1])
print("sorted alphabetically",b)

Output:

This is how to sort the list of tuples alphabetically in Python.

## Python sort list of tuples multiple keys

• Let us see how to sort the list of tuples including multiple keys in Python.
• By using the method bubble sort algorithm we can perform this particular task. In this example, each tuple is a key element in the given list and obtain the first element of each tuple using the nested loop method.

Example:

def mul_keys(key):

l = len(key)
for x in range(l):
for y in range(l-x-1):

if key[y][0] > key[y + 1][0]:
key[y], key[y + 1] = key[y + 1], key[y]
return key

key = [("Australia"), ("Switzerland"), ("Europe"),
("Paris"), ("Brazil")]
print("sorted multiple keys",mul_keys(key))

Here is the Screenshot of the following given code

## Python sort list of tuples by key

• In python programming, to sort the list of tuples by key we have the sort() method.
• The sort() is a built-in list method which sorts the elements of a given list.

Example:

Let’s take an example and check how to sort the list of tuples by key

my_list = [('Lily', 2), ('Rose', 7), ('Jasmine', 4), ('Lotus', 1)]

my_list.sort(key=lambda r: r[0])

print(my_list)

Check the Screenshot of the following given code

This is an example of a Python sort list of tuples by key.

## Python sort list of tuples by first

• Let us see how to sort the list of tuples by the first element in Python.
• In python to sort the list of tuples by the first element we have to use sorted() method with the argument key=lambda function.

Example:

n_lis = [('p', 34), ('a', 17), ('f', 16), ('i', 12)]

n_lis.sort(key=lambda u: u[0])

print(n_lis)

Here is the implementation of the following given code

The above Python code you can use to sort list of tuples by first element in Python.

## Python program to sort a list of tuples using lambda

In this example first, we will initialize a list of tuples and sort the list by their second third value in Python.

Example:

to_lis = [('z', 2, 19), ('b', 2, 24), ('c', 2, 28),
('q', 1, 10), ('p', 1, 19), ('o', 1, 15), ('e', 1, 17)]
new_list = sorted(to_lis, key=lambda m: (m[1],-m[2]))
print(new_list)

Output:

The above code we can use to sort a list of tuples using lambda in Python.

## Python sort list of tuples by second value descending

In Python to sort the list of tuples by second values in descending order, we have to use the Python sort() function.

Example:

new_lis = [('v', 2), ('u', 9), ('q', 10), ('w', 4)]

new_lis.sort(key=lambda i:i[1],reverse=True)
print("Second element descending order:",new_lis)

Here is the implementation of the following given code

This is how to sort list of tuples by second value in descending order in Python.

You may like the following Python tutorials:

In this tutorial, we have discussed how to sort list of tuples in python. We discussed the Python sort list of tuples with examples.

• Python sort list of tuples
• Python sort list of tuples descending
• Python sort list of tuples by last element
• Python sort list of tuples by first and second element
• Python sort list of tuples by multiple elements
• Python sort list of tuples by second value
• Python sort list of tuples alphabetically
• Python sort list of tuples multiple keys
• Python sort list of tuples by key
• Python sort list of tuples by first
• Python program to sort a list of tuples using lambda
• Python sort list of tuples by second value descending