Create an account


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.

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:


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 ($), 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!




https://www.sickgaming.net/blog/2022/04/...ond-value/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,556 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,695 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,512 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Easiest Way to Convert List of Hex Strings to List of Integers xSicKxBot 0 1,458 11-25-2022, 11:54 AM
Last Post: xSicKxBot
  [Tut] Python Find Shortest List in List xSicKxBot 0 1,372 09-25-2022, 03:42 AM
Last Post: xSicKxBot
  [Tut] Python Find Longest List in List xSicKxBot 0 1,303 09-23-2022, 02:19 PM
Last Post: xSicKxBot
  [Tut] How to Sort Words Alphabetically in Python? xSicKxBot 0 1,113 08-11-2022, 06:51 AM
Last Post: xSicKxBot
  [Tut] Convert CSV to List of Tuples in Python xSicKxBot 0 1,220 06-23-2022, 11:11 PM
Last Post: xSicKxBot
  [Tut] How to Convert List of Lists to Tuple of Tuples in Python? xSicKxBot 0 1,252 06-02-2022, 08:39 AM
Last Post: xSicKxBot
  [Tut] How to Convert Tuple of Tuples to List of Lists in Python? xSicKxBot 0 1,302 06-01-2022, 08:08 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016