Python | Sort a list according to the second element in sublistIn 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. Show
Different ways of Sorting the list of lists in python
1. Sorting the data by 1st columnTo 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)
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 listlast modified November 29, 2021 Python sort list tutorial shows how to sort list elements in Python language. 1. Default Python List sort() method usageBy 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 Answersorting by second element python by Protelr on Sep 24 2020 Comment 1 Add a Grepper Answer Python answers related to “how to sort a nested list in reverse order of second element”
Python queries related to “how to sort a nested list in reverse order of second element”
Python sort Lists ExamplesThe 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] |