Change language

# Python | Sort the list by the second item in the sublist

| | |

In this article, we will learn how to sort any list by the second element of the sublist present in the main list. We will see two ways to do this. We will explore three methods of doing this kind. One using Bubble Sort, one using the sort () method, and last but not least, using the sorted () method. In this program, we have sorted the list in ascending order.
Examples:

` Input: [[’rishav’, 10], [’ akash’, 5], [’ram’, 20], [’ gaurav’, 15]] Output: [[’akash’, 5], [’ rishav’, 10], [’gaurav’, 15], [’ ram’, 20]] Input: [[’452’, 10], [’ 256’, 5 ], [’100’, 20], [’ 135’, 15]] Output: [[’256’, 5], [’ 452’, 10], [’135’, 15], [’ 100’, 20]] `

Method 1: Using the Bubble Sort technique
Here we used the Bubble Sort to perform sorting. We tried to access the second item of the sublists using nested loops. This is done by the in-place sorting method. Time complexity is similar to bubble sort, i.e. O (n ^ 2)

 ` # Python code for sorting lists using the second element of sublists ` ` # Internal sorting, using third variable ` ` def ` ` Sort (sub_li): ` ` ` ` l ` ` = ` ` len ` ` (sub_li) ` ` ` ` for ` ` i ` ` in ` ` range ` ` (` ` 0 ` `, l): ` ` for ` ` j ` ` in ` ` range ` ` (` ` 0 ` `, l ` ` - ` ` i ` ` - ` ` 1 ` `): ` ` if ` ` (sub_li [j] [` ` 1 ` `]" sub_li [j ` ` + ` ` 1 ` `] [` ` 1 ` `]): ` ` tempo ` ` = ` ` sub_li [j] ` ` ` ` sub_li [j] ` ` = ` ` sub_li [j ` ` + ` ` 1 ` `] ` ` sub_li [j ` ` + ` ` 1 ` `] ` ` = ` ` tempo ` ` return ` ` sub_li `   ` Driver code ` < code class = "plain"> sub_li ` = ` ` [[[` ` ’rishav’ ` `, ` ` 10 ` `], [` `’ akash’ ` `, ` ` 5 ` `], [` ` ’ram’ ` `, ` ` 20 ` ` ], [` ` ’gaurav’ ` `, ` ` 15 ` `]] ` ` print ` ` (Sort (sub_li)) `

Output:

` [[’akash’, 5 ], [’rishav’, 10], [’ gaurav’, 15], [’ram’, 20]] `

Method 2: Sort using the sort () method
When sorting with this method, the actual content of the tuple is changed, and, as in the previous method, it does The in-place sorting method is used.

Output:

` [[’akash’, 5], [’ rishav’, 10], [’gaurav’, 15], [’ram’, 20]] `

Method 3: sorting using the sorted () method
Sorted () sorts the list and always returns the list with the sorted elements unchanged original sequence. It takes three parameters, of which two are optional, here we tried to use all three:

1. Iterable: sequence (list, tuple, string) or collection (dictionary, set, frozenset) or whatever the iterator to be sorted.
2. Key (optional): a function that will serve as the server’s key or base for sorting comparisons.
3. Reverse (optional): to sort this in ascending order , we could just ignore the third parameter we made in this program. If set to true, the iteration will be sorted in reverse (descending) order, by default it is set to false.

` `

 ` # Python code for sorting tuples using the second element ` ` Sublist # Internal sorting method using sort () ` ` def ` ` Sort (sub_li): `   ` # reverse = None (ascending sort) ` ` Key # set to sort using the second element ` ` # lambda list was used ` ` sub_li.sort (key ` ` = ` ` lambda ` ` x: x [` ` 1 ` `]) ` ` return ` ` sub_li `   ` Driver code ` ` sub_li ` ` = ` ` [[` `’ rishav’ ` `, ` ` 10 ` `], [` ` ’akash’ ` `, ` ` 5 ` `], [` ` ’ram’ ` `, ` ` 20 ` `], [` ` ’gaurav ’` `, ` ` 15 ` `]] ` ` print ` ` (Sort (sub_li)) `
 ` # Python code to sort tuples using the second element ` ` Sublist # Function to sort using sorted () ` ` def ` ` Sort (sub_li): ` ` `  ` # reverse = None ` ` ` ` Key # set to sort using second element ` ` # lambda list was used ` < code class = "keyword"> return ` (` ` sorted ` ` (sub_li, key ` ` = ` ` lambda ` ` x: x [` ` 1 ` `])) `   ` Driver code ` ` sub_li ` ` = ` ` [[` `’ rishav’ ` `, ` ` 10 ` `], [` ` ’akash’ ` `, ` ` 5 ` `], [` ` ’ram’ ` `, ` ` 20 ` `], [` ` ’gaurav’ ` ` , ` ` 15 ` `]] ` ` print ` ` (Sort (sub_li)) `
` `

` `

Output:

` [[’akash’, 5], [’ rishav’, 10], [’gaurav’, 15], [’ ram ’, 20]] `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries