
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort a List According to the Second Element in the Sublist
In this article, we will sort a list according to the second element in the sublist. Let's say we have the following list ?
[['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]]
The output should be the following i.e. sorted according to the second element ?
[['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]]
Python program to sort a list according to the second element in the sublist using the sort() method
Example
# Custom Function def SortFunc(sub_li): sub_li.sort(key = lambda x: x[1]) return sub_li # Driver Code subList =[['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]] print("Unsorted List = \n",subList) print("\nSorted List according to the second elemnt in the sublist =\n ",SortFunc(subList))
Output
Unsorted List = [['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]] Sorted List according to the second elemnt in the sublist = [['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]]
Python program to sort a list according to the second element in the sublist using Bubble Sort
Example
# Custom Function def SortFunc(subList): l = len(subList) for i in range(0, l): for j in range(0, l-i-1): if (subList[j][1] > subList[j + 1][1]): temp = subList[j] subList[j]= subList[j + 1] subList[j + 1]= temp return subList # Driver Code subList =[['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]] print("Unsorted List = \n",subList) print("\nSorted List according to the second elemnt in the sublist =\n ",SortFunc(subList))
Output
Unsorted List = [['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]] Sorted List according to the second elemnt in the sublist = [['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]]
Python program to sort a list according to the second element in the sublist using the sorted() method
Example
# Custom Function def SortFunc(subList): return(sorted(subList, key = lambda a: a[1])) # Driver Code subList =[['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]] print("Unsorted List = \n",subList) print("\nSorted List according to the second elemnt in the sublist =\n ",SortFunc(subList))
Output
Unsorted List = [['jack', 50], ['antony', 20], ['jones', 87], ['gary', 70], ['tom', 90], ['sam', 110], ['warner', 65]] Sorted List according to the second elemnt in the sublist = [['antony', 20], ['jack', 50], ['warner', 65], ['gary', 70], ['jones', 87], ['tom', 90], ['sam', 110]]
Advertisements