Problem Formulation and Solution Overview
In this article, you’ll learn how to sort a list of tuples by the second value in Python.
To make it more fun, we have the following running scenario:
BridgeTech is a bridge restoration company. They have asked you to sort and return the Top 10 elements from the Periodic Table based on the ‘Atomic Radius’ in descending order.
The atomic radius of a chemical element is a measure of the size of its atom, usually the mean or typical distance from the center of the nucleus to the outermost isolated electron.
Wikpedia
Click here to download the Periodic Table. Save this file as periodic_table.csv
and move it to the current working directory.
π¬ Question: How would you write the Python code to accomplish this task?
We can accomplish this task by one of the following options:
- Method 1: Use
sort()
andlambda
- Method 2: Use
sort()
anditemgetter()
- Method 3: Use
sorted()
andlambda
- Method 4: Use a Bubble Sort
Preparation
- The Pandas library enables access to/from a DataFrame.
To install this library, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
If the installation was successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import numpy as np from operator import itemgetter
π‘ Note: The operator
library is built-in to Python and does not require installation.
Method 1: Use Sort and a Lambda
To sort a list of tuples based on the second element, use sort()
and lambda
in the one-liner expression tups.sort(key=lambda x: x[1], reverse=True)
.
Here’s an example:
df = pd.read_csv('periodic_table.csv', usecols=['Name', 'AtomicRadius']) tups = [tuple(x) for x in df.values.tolist()] tups.sort(key=lambda x: x[1], reverse=True) print(tups[0:10])
The CSV file is read in preparation, and two (2) columns save to a DataFrame. The DataFrame then converts to a list of tuples (tups
) using List Comprehension.
We are ready to sort!
A lambda
is passed as a parameter to sort()
indicating the sort element (x[1]
), and the sort order is set to descending (reverse=True
). The results save to tups
.
To complete the process, slicing is performed, and the Top 10 elements are sent to the terminal.
Output
[('Francium', 348.0), ('Cesium', 343.0), ('Rubidium', 303.0), ('Radium', 283.0), ('Potassium', 275.0), ('Barium', 268.0), ('Actinium', 260.0), ('Strontium', 249.0), ('Curium', 245.0), ('Californium', 245.0)] |
Method 2: Use Sort & Itemgetter
To sort a list of tuples by the second element, use the sort()
and itemgetter()
functions in the expression tuples.sort(key=itemgetter(1), reverse=True)
.
Here’s an example:
df = pd.read_csv('periodic_table.csv', usecols=['Name', 'AtomicRadius']) tups = [tuple(x) for x in df.values.tolist()] tups.sort(key=itemgetter(1), reverse=True) print(tups[0:10])
The CSV file is read in preparation, and two (2) columns save to a DataFrame. The DataFrame then converts to a List of Tuples (tups
) using List Comprehension.
We are ready to sort!
The sort()
function passes a key (itemgetter(n)
) where n
is the sort element (itemgetter(1)
), and the sort order is set to descending (reverse=True
).
The results save to tups
.
To complete the process, slicing is performed, and the Top 10 elements are sent to the terminal.
π‘ Note: The itemgetter()
function is slightly faster than a lambda
. Use itemgetter
if speed and memory are a factor.
Method 3: Use Sorted & Lambda
To sort a list of tuples by the second element, combine the functions sorted()
and lambda
in the expression sorted(tups, key=lambda x:(x[1]), reverse=True)
and assign the resulting sorted list to the original variable tups
.
Here’s an example:
df = pd.read_csv('periodic_table.csv', usecols=['Name', 'AtomicRadius']) tups = [tuple(x) for x in df.values.tolist()] tups = sorted(tups, key=lambda x:(x[1]), reverse=True) print(tups[0:10])
The CSV file is read in preparation, and two (2) columns save to a DataFrame. The DataFrame then converts to a List of Tuples (tups
) using List Comprehension.
We are ready to sort!
A lambda
is passed as a parameter to sorted()
, indicating the sort element (x[1]
), and the sort order is set to descending (reverse=True
). The results save to tups
.
To complete the process, slicing is performed, and the Top 10 elements are sent to the terminal.
Method 4: Use Bubble Sort
To sort a List of Tuples by the second element, you can also modify a sorting algorithm from scratch such as Bubble Sort to access the second (or n-th) tuple value as a basis for sorting.
Here’s an example:
df = pd.read_csv('periodic_table.csv', usecols=['Name', 'AtomicRadius']) tups = [tuple(x) for x in df.values.tolist()] def sort_tuples_desc(tups, idx): length = len(tups) for i in range(0, length): for j in range(0, length-i-1): if (tups[j][idx] < tups[j + 1][idx]): tmp = tups[j] tups[j] = tups[j+1] tups[j+1] = tmp return tups print(sort_tuples_desc(tups, 1)[0:10])
The CSV file is read in preparation, and two (2) columns save to a DataFrame. The DataFrame then converts to a List of Tuples (tups
) using List Comprehension.
We are ready to sort!
A sort function sort_tuples_desc
is created and passed two (2) parameters: a List of Tuples (tups
), and the sort element (idx
). Then, the infamous Bubble Sort is performed on the elements.
This function returns a List of Tuples sorted in descending order.
To complete the process, slicing is performed, and the Top 10 elements are sent to the terminal.
Summary
These four (4) methods of sorting a List of Tuples based on the second element should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!