How do you sort a nested list in descending order Python?

Python | Sort a list according to the second element in sublist

In this article, we will learn how to sort any list, according to the second element of the sublist present within the main list. We will see two methods of doing this. We will learn three methods of performing this sort. One by the use of Bubble Sort, second by using the sort() method and last but not the least by the use of 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]]

Different ways of Sorting the list of lists in python

  1. Sorting the data by 1st column
  2. Sorting the data using any other column
  3. Sorting the list of lists by length
  4. How to sort the list of lists by the sum of elements
  5. Sorting the list of lists in descending order
  6. Creating our own Program to sort the list of lists in Python

1. Sorting the data by 1st column

To sort the data according to the 1st columns in ascending order.

list1=[['Bajaj Pulsar','220 F',40],['Yamaha','YZF R15 Ver 3.0',45],['TVS Apache','rtr 160',50]] # Using the key parameter of sort method. # if we use the sort method, the sorting take place in the list itself list1.sort() print(list1)
How do you sort a nested list in descending order Python?

Output-

[['Bajaj Pulsar', '220 F', 40], ['TVS Apache', 'rtr 160', 50], ['Yamaha', 'YZF R15 Ver 3.0', 45]]

When we want to sort the list according to the first columns, we do not need to do anything special. We just need to do everything as usual. Let us see another way of achieving the same.

The above method sorts the data in the original list itself. If we do not wish to make the changes in the original list, we can use the below method.

list1=[['Bajaj Pulsar','220 F',40],['Yamaha','YZF R15 Ver 3.0',45],['TVS Apache','rtr 160',50]] print(sorted(list1)) # lets see if our original list has changed or not print(list1)
[['Bajaj Pulsar', '220 F', 40], ['TVS Apache', 'rtr 160', 50], ['Yamaha', 'YZF R15 Ver 3.0', 45]]

Now let us see the naive method using to achieve our goal.

list1=[['Bajaj Pulsar','220 F',40],['Yamaha','YZF R15 Ver 3.0',45],['TVS Apache','rtr 160',50]] # Iterating through one the list. #There is no element with which we will compare the last element, #so we are not including the last element for i in range(len(list1)-1): # if the next element is greater then the next element, swap it. if list1[i][0]>list1[i+1][0]: list1[i][0],list1[i+1][0]=list1[i+1][0],list1[i][0] print(list1)
[['Bajaj Pulsar', '220 F', 40], ['TVS Apache', 'YZF R15 Ver 3.0', 45], ['Yamaha', 'rtr 160', 50]]

Python sort list

last modified November 29, 2021

Python sort list tutorial shows how to sort list elements in Python language.

1. Default Python List sort() method usage

By default, the list sort() method in Python arranges the elements of a list in ascending order. This also is the natural way of sorting elements.

numbers_list = [3.4, 5.1, 2.2, 4.1, 1.0, 3.8] print(f'Before sorting: {numbers_list}') numbers_list.sort() print(f'After sorting: {numbers_list}')

Output:

Before sorting: [3.4, 5.1, 2.2, 4.1, 1.0, 3.8] After sorting: [1.0, 2.2, 3.4, 3.8, 4.1, 5.1]

Recommended Read: Python f-strings

The elements can also be characters or numbers and the sort() method will continue to sort in ascending order.

str_list = ['a', 'c', 'd', 'b', 'B', 'C', '1'] str_list.sort() print(str_list) # ['1', 'B', 'C', 'a', 'b', 'c', 'd']

“how to sort a nested list in reverse order of second element” Code Answer


sorting by second element
python by Protelr on Sep 24 2020 Comment
1
Add a Grepper Answer

  • how to flip a list backwards in python
  • python show only 1st element of nested lists
  • how to sort a list descending python
  • reverse element in a list in python 3
  • how to sort list in descending order in python
  • python sort list in reverse
  • how to sort a list of dictionary by value in descending order?
  • reverse a list using slice operator
  • list slicing reverse python
  • print list in reverse order python
  • python sort list in reverse order
  • sort first element reverse sort second python
  • sort and reverse list in python
  • how to sort nested list in python
  • how to find the lowest value in a nested list python

  • sorting a nested list in python
  • sort by second element python
  • sort list in list
  • sorting nested list according to number of elements python
  • sort list of list
  • python dsort list of list by second value highest to smalles
  • sorting nested lists in python
  • how to sort nested lists in python
  • python sort array of arrays by second element
  • sorting a list of list
  • sort list by sublist python
  • sort a nested list python
  • sort sublist python with the element in second index position
  • sorted list by second element python
  • how to sort the lists inside of a list based on the second element
  • python sort by second elelment
  • get second element of a sorted array python with order of n
  • sort a list according to the second list in sublist
  • sort a list based on its 2nd valeue
  • how to sort a nested list in reverse order of second element
  • python sort list of lists by second characters
  • sort 2nd index of list python
  • how to sort a dictionary in according to a second element
  • every second element in list python in descreasing order
  • print every second element of list python in decreasing order
  • sort list of list in python
  • python list sort second element
  • python desort list of list by second value
  • sort list subelement
  • python sort list by nested value
  • sort list of lists by second element python
  • sorting sublist in python
  • how to sort nested list in python
  • how to sort a nested list in python
  • how to sort a 2d list in python
  • python sort by 2nd element
  • python sort nested list
  • nested list sort python
  • sort elemts according to a given list
  • sort a list according to the second element in sublist
  • sorting nested list in python
  • sorted nested list python
  • how to sort a list in according to a second element
  • python sort nested list by value
  • python bubble sort list second order
  • list of lists sorting on 2nd postion
  • sort list by 2nd element
  • order list based on element of list python
  • get second element of a sorted array python
  • sort using second element in array
  • sorting a list by an element
  • sort by second value list python
  • how to sort a list by teh second element of the inside list
  • sorting lists of lists python
  • sort same elements in a list python
  • sort list of list based on second element
  • sort list of list by second element python
  • order nested list python
  • python sort list of list by second value
  • sort list by second element python descending
  • python sort nested list by second element
  • how to sort list of list in python
  • python sort list by element
  • sort list in list python
  • sort nested list python
  • sorting a list based on the second element in the lists
  • how to sort list by second place in python
  • python sort a list of lists by second element
  • sorting by second element
  • python sort by second element
  • write a function to sort the list based on the first letter of the second element
  • python sort list of lists by second element
  • soritng a list of list in python
  • python sort list of list by second element
  • python sort list by second element
  • sorted by second element python
  • sort list of list using first value in acending and second value in desc
  • how to sort array according to second element
  • sort list by the second element python
  • sort lisr according to second element
  • sort list according to second element
  • how to sort a 2d list in python according to single dimension list
  • sorted in python what is second element
  • map sort by second element python
  • nested list sorting in python
  • how to sort list of lists by second element python
  • how to sort list by nested list in python
  • sort list by second element python
  • python sort list by sublist element
  • sort list based on second element python
  • write a python code to sort a nested list
  • python sort by second element in list
  • sort by second index python
  • sort by second element in list python
  • how to sort list of lists in python
  • sort based on second element

Python sort Lists Examples

The following Python examples show you how to sort integer list items in ascending and Descending order.

This python function sorts the given list in ascending order. The below code organize the elements in an integer list.

a = [10, 190, 120, 180, 120, 105] a.sort() print(a)10, 105, 120, 120, 180, 190]

The sort function has one argument called reverse = True which allows us to organize the list items in descending order. The following code sorts the integer list elements in Descending order or reverse order.

TIP: Please refer to List article and methods article to understand everything about them in Python. And also refer to len, min, and max.

lexm = [13, 98, 125, 191, 22, 9, 91, 78, 242] print("\n Original = ", exm) exm.sort() print("Ascending = ", exm) exm2 = [13, 98, 125, 191, 22, 9, 91, 78, 242] print("\n Original exm2 = ", exm2) exm2.sort(reverse = True) print("Descending = ", exm2) Original = [13, 98, 125, 191, 22, 9, 91, 78, 242] Ascending = [9, 13, 22, 78, 91, 98, 125, 191, 242] Original exm2 = [13, 98, 125, 191, 22, 9, 91, 78, 242] Descending = [242, 191, 125, 98, 91, 78, 22, 13, 9]