Explain Codes LogoExplain Codes Logo

How to sort a list/tuple of lists/tuples by the element at a given index

python
performance
best-practices
dataframe
Nikita BarsukovbyNikita Barsukov·Jan 1, 2025
TLDR

Quickly sort a list/tuple at a specific index using Python's sorted() function and applying a lambda function as the key:

data = [('pear', 3), ('apple', 1), ('banana', 2)] sorted_data = sorted(data, key=lambda item: item[1]) # Index 1 for sorting print(sorted_data) # [('apple', 1), ('banana', 2), ('pear', 3)]

This command sorts data based on the numeric value found at index 1 within each tuple.

Boost sorting with itemgetter

Achieve higher performance with operator.itemgetter, a speed-focused alternative to lambda expressions. Ideal for index-based sorting — and easily converted for reverse sorting:

from operator import itemgetter sorted_data = sorted(data, key=itemgetter(1)) # Watch me run faster than a lambda!

itemgetter(1) creates a function that pulls the element at index 1. Similar to lambda functions, it comes with less baggage.

Sorting complex data structures

With lists of lists or tuples testing your sorting skills, lambdas put a spotlight on your prowess by providing diverse expressions:

# Multi-criteria sorting: First by second element, next by the third sorted_data = sorted(data, key=lambda item: (item[1], item[2])) # Because one criterion is too mainstream

This command queues data elements based on the second element, then, for good measure, ranks them by the third element if they match.

Picking the right data structure

Tuples or lists? A tough choice that wields influence over sort performance. If your dataset is dynamic, go for lists. Use tuples when data is consistent, and memory conservation shines high on your priority list.

With bigger fishes (datasets) to fry, those once efficient sorting algorithms may need a tune-up. It calls for a continual review and optimization of methodologies — from the key functions to the data structures used.

Sorting in reverse - not talking about treadmills

Python’s sort() and sorted() functions come with a friendly tag-along reverse=True, perfect for sorting from largest to smallest:

sorted_data_desc = sorted(data, key=lambda item: item[1], reverse=True) # Welcome to the Upside Down

This tactic saves effort and eliminates redundant post-sorting operations like reversing.

Master-class sorting

Complex sorting scenarios call for custom comparison functions utilizing the cmp_to_key wrapper from functools:

from functools import cmp_to_key def custom_compare(x, y): # Custom comparison logic... return ... data.sort(key=cmp_to_key(custom_compare))

Note that these custom comparisons are less efficient and must only be called to duty when the situation desperately needs it.

Sailing through data growth

As your dataset blossoms, your beloved sorting algorithm might struggle to keep up. Regular performance check-ups are vital. Such assessments might spring up the need for a different tactic, like hybrid algorithms or even the big guns — indexing mechanisms for titan-sized datasets.