Python | Sort the list by the second item in the sublist

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

Method 1: Using the Bubble Sort technique
Here we used the Bubble Sort to perform sorting. We tried to access the second item of the sublists using nested loops. This is done by the in-place sorting method. Time complexity is similar to bubble sort, i.e. O (n ^ 2)

# Python code for sorting lists using the second element of sublists
# Internal sorting, using third variable

def Sort (sub_li):

  l = len (sub_li)

  for i in range ( 0 , l):

for j in range ( 0 , l - i - 1 ):

if (sub_li [j] [ 1 ] & gt; sub_li [j + 1 ] [ 1 ]):

tempo = sub_li [j]

  sub_li [j] = sub_li [j + 1 ]

sub_li [j + 1 ] = tempo

return sub_li

 
Driver code

< code class = "plain"> sub_li = [[[ `rishav` , 10 ], [ ` akash` , 5 ], [ `ram` , 20 ], [ `gaurav` , 15 ]]

print (Sort (sub_li))

Output:

 [[`akash`, 5 ], [`rishav`, 10], [` gaurav`, 15], [`ram`, 20]] 

Method 2: Sort using the sort () method
When sorting with this method, the actual content of the tuple is changed, and, as in the previous method, it does The in-place sorting method is used.

Output:

 [[`akash`, 5], [` rishav`, 10], [`gaurav`, 15], [`ram`, 20]] 

Method 3: sorting using the sorted () method
Sorted () sorts the list and always returns the list with the sorted elements unchanged original sequence. It takes three parameters, of which two are optional, here we tried to use all three:

  1. Iterable: sequence (list, tuple, string) or collection (dictionary, set, frozenset) or whatever the iterator to be sorted.
  2. Key (optional): a function that will serve as the server`s key or base for sorting comparisons.
  3. Reverse (optional): to sort this in ascending order , we could just ignore the third parameter we made in this program. If set to true, the iteration will be sorted in reverse (descending) order, by default it is set to false.

# Python code for sorting tuples using the second element
Sublist # Internal sorting method using sort ()

def Sort (sub_li):

 

# reverse = None (ascending sort)

Key # set to sort using the second element

# lambda list was used

sub_li.sort (key = lambda x: x [ 1 ])

return sub_li

 
Driver code

sub_li = [[ ` rishav` , 10 ], [ `akash` , 5 ], [ `ram` , 20 ], [ `gaurav ` , 15 ]]

print (Sort (sub_li))

# Python code to sort tuples using the second element
Sublist # Function to sort using sorted ()

def Sort (sub_li):

  

# reverse = None

  Key # set to sort using second element

# lambda list was used

< code class = "keyword"> return ( sorted (sub_li, key = lambda x: x [ 1 ])) 

 
Driver code

sub_li = [[ ` rishav` , 10 ], [ `akash` , 5 ], [ `ram` , 20 ], [ `gaurav` , 15 ]]

print (Sort (sub_li))

Output:

 [[`akash`, 5], [` rishav`, 10], [`gaurav`, 15], [` ram `, 20]]