Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Sort a List of Tuples by Second Value
1
How to Sort a List of Tuples by Second Value

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.

Quote: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
Code:
periodic_table.csv
and move it to the current working directory.


[Image: 1f4ac.png] Question: How would you write the Python code to accomplish this task?

We can accomplish this task by one of the following options:

Preparation

Before any data manipulation can occur, one (1) new library will require installation.
  • The Pandas library enables access to/from a DataFrame.

To install this library, navigate to an IDE terminal. At the command prompt (
Code:
$
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign (
Code:
$
). Your terminal prompt may be different.

Code:
$ pip install pandas

Hit the
Code:
<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.

Code:
import numpy as np from operator import itemgetter

[Image: 1f4a1.png] Note: The
Code:
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
Code:
sort()
and
Code:
lambda
in the one-liner expression
Code:
tups.sort(key=lambda x: x[1], reverse=True)
.

Here’s an example:

Code:
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 (
Code:
tups
) using List Comprehension.

We are ready to sort!

A https://blog.finxter.com/a-simple-introd...in-python/
Code:
lambda
is passed as a parameter to
Code:
sort()
indicating the sort element (
Code:
x[1]
), and the sort order is set to descending (
Code:
reverse=True
). The results save to
Code:
tups
.

To complete the process, slicing is performed, and the Top 10 elements are sent to the terminal.

Output

Code:
[('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
Code:
sort()
and
Code:
itemgetter()
functions in the expression
Code:
tuples.sort(key=itemgetter(1), reverse=True)
.

Here’s an example:

Code:
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 (
Code:
tups
) using List Comprehension.

We are ready to sort!

The
Code:
sort()
function passes a key (
Code:
itemgetter(n)
) where
Code:
n
is the sort element
Code:
(itemgetter(1)
), and the sort order is set to descending (
Code:
reverse=True
).

The results save to
Code:
tups
.

To complete the process, slicing is performed, and the Top 10 elements are sent to the terminal.

[Image: 1f4a1.png] Note: The
Code:
itemgetter()
function is slightly faster than a
Code:
lambda
. Use
Code:
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
Code:
sorted()
and
Code:
lambda
in the expression
Code:
sorted(tups, key=lambda x:(x[1]), reverse=True)
and assign the resulting sorted list to the original variable
Code:
tups
.

Here’s an example:

Code:
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 (
Code:
tups
) using List Comprehension.

We are ready to sort!

A
Code:
lambda
https://blog.finxter.com/a-simple-introd...in-python/is passed as a parameter to
Code:
sorted()
, indicating the sort element (
Code:
x[1]
), and the sort order is set to descending (
Code:
reverse=True
). The results save to
Code:
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:

Code:
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 (
Code:
tups
) using List Comprehension.

We are ready to sort!

A sort function
Code:
sort_tuples_desc
is created and passed two (2) parameters: a List of Tuples (
Code:
tups
), and the sort element (
Code:
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!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: