Select Page

## Thủ Thuật Hướng dẫn Python sort list of tuples by both elements Chi Tiết

Pro đang tìm kiếm từ khóa Python sort list of tuples by both elements được Update vào lúc : 2022-01-02 11:05:50 . Với phương châm chia sẻ Mẹo về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi đọc nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Ad lý giải và hướng dẫn lại nha.

HowToPython How-To’sSort List of Tuples in Python

## Sort List of Tuples in Python

Python Python Tuple

Created: February-26, 2022 | Updated: October-17, 2022

Nội dung chính

Sort List of Tuples in PythonUse the list.sort() Function to Sort List of Tuples in PythonUse the Bubble Sort Algorithm to Sort the List of Tuples in PythonRelated Article – Python TupleVideo liên quan

In Python, multiple items can be stored in a single variable using Tuples. A list of Tuples can be sorted like a list of integers.

This tutorial will discuss different methods to sort a list of tuples based on the first, second, or ith element in the tuples.

## Use the list.sort() Function to Sort List of Tuples in Python

The list.sort() function sorts the elements of a list in ascending or descending order. Its key parameter specifies the value to be used in the sorting. key shall be a function or other callables that could be applied to each list element.

The following code sorts the tuples based on the second element in all the tuples.

list_students = [(‘Vaibhhav’,86), (‘Manav’,91) , (‘Rajesh’,88) , (‘Sam’,84), (‘Richie’,89)]
#sort by second element of tuple
list_students.sort(key = lambda x: x[1]) #index 1 means second element
print(list_students)

Output:

[(‘Sam’,84), (‘Vaibhhav’,86), (‘Rajesh’,88), (‘Richie’,89), (‘Manav’,91)]

The order can be reversed to descending by setting the reverse parameter of the sort() method to be True.

The following code sorts the list of tuples in descending order by using the reverse parameter.

list_students = [(‘Vaibhhav’,86), (‘Manav’,91) , (‘Rajesh’,88) , (‘Sam’,84), (‘Richie’,89)]
#sort by second element of tuple
list_students.sort(key = lambda x: x[1], reverse=True)
print(list_students)

Output:

[(‘Manav’,91), (‘Richie’,89), (‘Rajesh’,88), (‘Vaibhhav’,86), (‘Sam’,84)]

## Use the Bubble Sort Algorithm to Sort the List of Tuples in Python

Bubble Sort is one of the simplest sorting algorithms; it works by swapping the adjacent elements in a list if they are in the wrong order and repeats this step until the list is sorted.

The following code sorts the tuples based on the second element and uses the bubble sort algorithm.

list_ = [(‘Vaibhhav’,86), (‘Manav’,91) , (‘Rajesh’,88) , (‘Sam’,84), (‘Richie’,89)]
#sort by second element of tuple
pos = 1
list_length = len(list_)
for i in range(0, list_length):
for j in range(0, list_length-i-1):
if (list_[j][pos] > list_[j + 1][pos]):
temp = list_[j]
list_[j]= list_[j + 1]
list_[j + 1]= temp
print(list_)

Output:

[(‘Sam’,84), (‘Vaibhhav’,86), (‘Rajesh’,88), (‘Richie’,89), (‘Manav’,91)]

The pos variable specifies the position by which sorting has to be carried out, which in this case, is the second element.

We can also use the first element to sort the list of tuples. The following program implements this.

list_ = [(‘Vaibhhav’,86), (‘Manav’,91) , (‘Rajesh’,88) , (‘Sam’,84), (‘Richie’,89)]
#sort by first element of tuple
pos = 0
list_length = len(list_)
for i in range(0, list_length):
for j in range(0, list_length-i-1):
if (list_[j][pos] > list_[j + 1][pos]):
temp = list_[j]
list_[j]= list_[j + 1]
list_[j + 1]= temp
print(list_)

Output:

[(‘Manav’,91), (‘Rajesh’,88), (‘Richie’,89), (‘Sam’,84), (‘Vaibhhav’,86)]
ContributeDelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

## Related Article – Python Tuple

Convert Tuple to String in Python

Reverse a String in PythonString Builder Equivalent in Python

4
0
Chia sẻ

### Review Python sort list of tuples by both elements ?

Bạn vừa tìm hiểu thêm tài liệu Với Một số hướng dẫn một cách rõ ràng hơn về Review Python sort list of tuples by both elements tiên tiến và phát triển nhất

### Chia Sẻ Link Down Python sort list of tuples by both elements miễn phí

You đang tìm một số trong những Chia Sẻ Link Cập nhật Python sort list of tuples by both elements Free.

#### Giải đáp vướng mắc về Python sort list of tuples by both elements

Nếu sau khi đọc nội dung bài viết Python sort list of tuples by both elements vẫn chưa hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Mình lý giải và hướng dẫn lại nha
#Python #sort #list #tuples #elements