Program to Sort the List According to the Second Element in Sublist

Python Program to Sort the List According to the Second Element in Sublist

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given list of lists which contain two elements in every sub list , the task is to sort the lists according to the second element in sub list.

Examples:

Example1:

Input:

given nested list =[['hello', 11], ['this', 1], ['is', 23], ['btechgeeks', 19], ['online', 39], ['platform', 7], ['for', 29]]

Output:

printing the sorted nested list before sorting :
[['hello', 11], ['this', 1], ['is', 23], ['btechgeeks', 19], ['online', 39], ['platform', 7], ['for', 29]]
printing the sorted nested list after sorting :
[['this', 1], ['platform', 7], ['hello', 11], ['btechgeeks', 19], ['is', 23], ['for', 29], ['online', 39]]

Example2:

Input:

[['hello', 46], ['this', 31], ['morning', 29], ['is', 26], ['btechGeeks', 19], ['online', 33]]

Output:

printing the sorted nested list before sorting :
[['hello', 46], ['this', 31], ['morning', 29], ['is', 26], ['btechGeeks', 19], ['online', 33]]
printing the sorted nested list after sorting :
[['btechGeeks', 19], ['is', 26], ['morning', 29], ['this', 31], ['online', 33], ['hello', 46]]

Example3:

Input:

[['sky', 12], ['is', 39], ['blue', 5]]

Output:

printing the sorted nested list before sorting :
[['sky', 12], ['is', 39], ['blue', 5]]
printing the sorted nested list after sorting :
[['blue', 5], ['sky', 12], ['is', 39]]

Program to Sort the List According to the Second Element in Sub list in Python

Below are the ways to sort the given list of lists according to the second element in sub list in python some of them are:

Method #1: Using nested loops and temporary variable(Static Input)

Approach:

  • Take in a list that contains sublists as static input.
  • Using two for loops, sort the sublists using bubble sort based on the second value of the sublist.
  • If the second element of the first sublist is greater than the second element of the second sublist, the complete sublist should be exchanged/swapped.
  • The sorted list should be printed.
  • Exit of program.

Below is the implementation:

# Take in a list that contains sublists as static input.
nestedList = [['hello', 46], ['this', 31], ['morning', 29],
              ['is', 26], ['btechGeeks', 19], ['online', 33]]
# printing the sorted nested list before sorting
print('printing the sorted nested list before sorting :')
print(nestedList)
# Using two for loops, sort the sublists using bubble
# sort based on the second value of the sublist.
for m in range(len(nestedList)):
    for n in range(len(nestedList)-m-1):
      # If the second element of the first sublist is greater than the second element of the second sublist,
      # the complete sublist should be exchanged/swapped using temporary variable
        if(nestedList[n][1] > nestedList[n+1][1]):
            tempo = nestedList[n]
            nestedList[n] = nestedList[n+1]
            nestedList[n+1] = tempo
# printing the sorted nested list after sorting
print('printing the sorted nested list after sorting :')
print(nestedList)

Output:

printing the sorted nested list before sorting :
[['hello', 46], ['this', 31], ['morning', 29], ['is', 26], ['btechGeeks', 19], ['online', 33]]
printing the sorted nested list after sorting :
[['btechGeeks', 19], ['is', 26], ['morning', 29], ['this', 31], ['online', 33], ['hello', 46]]

Method #2: Using nested loops and temporary variable(User Input)

Approach:

  • Take a empty list.
  • Scan the number of elements /number of sublists as user input using int(input()) function.
  • Loop from 1 to given number of elements times using for loop.
  • Scan the list elements using input() and int(input()) functions and append to the empty list using append() function.
  • Using two for loops, sort the sublists using bubble sort based on the second value of the sublist.
  • If the second element of the first sublist is greater than the second element of the second sublist, the complete sublist should be exchanged/swapped.
  • The sorted list should be printed.
  • Exit of program.

Below is the implementation:

# Take a empty list.
nestedList = []
# Scan the number of elements /number of sublists as user input using int(input()) function.
totalsublists = int(input('Enter some random number of sublists ='))
# Loop from 1 to given number of elements times using for loop.
for r in range(totalsublists):
    # Scan the list elements using input() and int(input()) functions and append
    # to the empty list using append() function.
    firsteleme = input('Enter some random first element =')
    secondeleme = int(input('Enter some random second element ='))
    nestedList.append([firsteleme, secondeleme])

# printing the sorted nested list before sorting
print('printing the sorted nested list before sorting :')
print(nestedList)
# Using two for loops, sort the sublists using bubble
# sort based on the second value of the sublist.
for m in range(len(nestedList)):
    for n in range(len(nestedList)-m-1):
      # If the second element of the first sublist is greater than the second element of the second sublist,
      # the complete sublist should be exchanged/swapped using temporary variable
        if(nestedList[n][1] > nestedList[n+1][1]):
            tempo = nestedList[n]
            nestedList[n] = nestedList[n+1]
            nestedList[n+1] = tempo
# printing the sorted nested list after sorting
print('printing the sorted nested list after sorting :')
print(nestedList)

Output:

Enter some random number of sublists =7
Enter some random first element =hello
Enter some random second element =11
Enter some random first element =this
Enter some random second element =1
Enter some random first element =is
Enter some random second element =23
Enter some random first element =btechgeeks
Enter some random second element =19
Enter some random first element =online
Enter some random second element =39
Enter some random first element =platform
Enter some random second element =7
Enter some random first element =for
Enter some random second element =29
printing the sorted nested list before sorting :
[['hello', 11], ['this', 1], ['is', 23], ['btechgeeks', 19], ['online', 39], ['platform', 7], ['for', 29]]
printing the sorted nested list after sorting :
[['this', 1], ['platform', 7], ['hello', 11], ['btechgeeks', 19], ['is', 23], ['for', 29], ['online', 39]]

Explanation:

  • The user must enter a list with numerous sublists.
  • Scan the list elements using input() and int(input()) functions and append to the empty list using append() function.
  • The list is then sorted using bubble sort based on the second member in the sublist.
  • The entire sublist is switched if the second element of the first sublist is bigger than the second element of the second sublist.
  • This step is repeated until the full list has been sorted.
  • After that, the sorted list is printed.

Related Programs: