This is a Python Program to sort the list according to the second element in the sublist.
The program takes a list of lists and sorts the list according to the second element in the sublist.
1. Take in a list containing sublists.
2. Using two for loops, use bubble sort to sort the sublists based on the second value of the sublist.
3. If the second element of the first sublist is greater than the second element of the second sublist, exchange the entire sublist.
4. Print the sorted list.
5. Exit.
Here is source code of the Python Program to sort the list according to the second element in the sublist. The program output is also shown below.
a=[['A',34],['B',21],['C',26]] for i in range(0,len(a)): for j in range(0,len(a)-i-1): if(a[j][1]>a[j+1][1]): temp=a[j] a[j]=a[j+1] a[j+1]=temp print(a)
1. User must enter a list containing several sublists.
2. Then bubble sort is implemented to sort the list according to the second element in the sublist.
3. If the second element of the first sublist is greater than the second element of the second sublist, then the entire sublist is switched.
4. This process continues till the entire list has been sorted.
5. The sorted list is then printed.
Case 1: [['B', 21], ['C', 26], ['A', 34]]
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Apply for Programming Internship
- Check Python Books
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Python Internship