Python program to sort a list according to the second element in the sublist.

In this article, we will learn how to sort a list according to the second element in each sublist. This is useful when working with data where each item is a pair, such as name-score pairs or key-value combinations.

Let's say we have the following list ?

[['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]]

The output should be sorted according to the second element ?

[['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]]

Using sort() Method

The sort() method sorts the list in-place using a lambda function to specify the sorting key ?

# Custom Function
def sort_by_second(sub_li):
    sub_li.sort(key=lambda x: x[1])
    return sub_li
    
# Driver Code
student_scores = [['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]]
print("Unsorted List =")
print(student_scores)
print("\nSorted List according to the second element in the sublist =")
print(sort_by_second(student_scores))
Unsorted List =
[['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]]

Sorted List according to the second element in the sublist =
[['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]]

Using sorted() Method

The sorted() method returns a new sorted list without modifying the original ?

# Custom Function
def sort_by_second(student_data):
    return sorted(student_data, key=lambda a: a[1])

# Driver Code
student_scores = [['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]]
print("Unsorted List =")
print(student_scores)
print("\nSorted List according to the second element in the sublist =")
print(sort_by_second(student_scores))
Unsorted List =
[['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]]

Sorted List according to the second element in the sublist =
[['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]]

Using Bubble Sort

We can also implement a custom bubble sort algorithm to sort by the second element ?

# Custom Function
def bubble_sort_by_second(student_data):
    n = len(student_data)
    for i in range(0, n):
        for j in range(0, n - i - 1):
            if student_data[j][1] > student_data[j + 1][1]:
                temp = student_data[j]
                student_data[j] = student_data[j + 1]
                student_data[j + 1] = temp
    return student_data

# Driver Code
student_scores = [['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]]
print("Unsorted List =")
print(student_scores)
print("\nSorted List according to the second element in the sublist =")
print(bubble_sort_by_second(student_scores))
Unsorted List =
[['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]]

Sorted List according to the second element in the sublist =
[['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]]

Comparison

Method Modifies Original Time Complexity Best For
sort() Yes O(n log n) In-place sorting
sorted() No O(n log n) Keep original unchanged
Bubble Sort Yes O(n²) Educational purposes

Conclusion

Use sorted() when you need to preserve the original list. Use sort() for in-place sorting with better memory efficiency. Both built-in methods are preferred over bubble sort for performance.

Updated on: 2026-03-24T21:04:46+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements