  # Python program to sort a list of tuples by second element

Loops | Python Methods and Functions | String Variables

Examples :

`  Input:  [(`for`, 24), (` Geeks`, 8), (`Geeks`, 30)]  Output:  [(`Geeks`, 8), (` for`, 24), (`Geeks`, 30)]  Input:  [(` 452 `, 10), (` 256`, 5), (`100`, 20), (` 135`, 15)]  Output:  [(`256`, 5), (` 452 `, 10), (` 135`, 15), (`100`, 20)] `

Method # 1: Using Bubble Sort

Using the Bubble Sort technique, we can perform sorting. Note that each tuple is an element in the given list. Access the second element of each tuple 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 program to sort the list of tuples by the second element `   ` # Function for sorting the list of tuples by the second element ` ` def ` ` Sort_Tuple (tup): ` `  `` # get the length of the list of tuples   lst = len (tup)  for i in range ( 0 , lst):    for j in range ( 0 , lst - i - 1 ):  if (tup [j] [ 1 ] & gt; tup [j + 1 ] [ 1 ]):  temp = tup [j]    tup [j] = tup [j + 1 ]  tup [j + 1 ] = temp  return tup    Driver code t up = [( `for` , 24 ), ( `is` , 10 ), ( ` Geeks` , 28 ),  ( `Geeksforgeeks` , 5 ), ( `portal` , 20 ), ( ` a` , 15 )]    print (Sort_ Tuple (tup)) `

Output:

` [(`Geeksforgeeks`, 5), (` is`, 10), (`a`, 15), (` portal`, 20), (`for`, 24), (`Geeks`, 28)] `

Method # 2: Using of the sort () method

Sorting with this method changes the actual content of the tuple and, as in the previous method, executes the sort in place method.

` `

` # Python program to sort the list # tuples by the second Item using sort ()   # Function for sorting the list by the second element of the tuple def Sort_Tuple (tup):     # reverse = None (sort in ascending order) Key # set to sort using second element # lambda list was used tup.sort (key = lambda x: x [ 1 ])  return tup     Driver code tup = [( `rishav` , 10 ), ( `akash` , 5 ), ( ` ram` , 20 ), ( `gaurav` , 15 ) ]    # print a sorted list of tuples print (Sort_Tuple (tup))  `

Exit:

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

Method # 3: Using sorted () method

Sorted () method sorts the list and that`s it where returns a list with sorted elements without changing the original sequence. It takes three parameters, of which two are optional, here we tried to use all three:

Iterable: sequence (list, tuple, string) or collection (dictionary, set, frozenset ) or any other iterator that needs to be sorted.
Key (optional): a function that will serve as a key or basis for sorting comparisons.
Reverse (optional): to sort this in ascending order, we could simply ignore the third parameter we did in this program. If set to true, the iteration will be sorted in reverse (descending) order, by default it is set to false.

 ` # Python program to sort the list ` ` # tuples on the second element using sorted () `   ` # Function for sorting the list by the second element of the tuple ` ` def ` ` Sort_Tuple (tup): `   ` # reverse = None (sort in ascending order) ` ` Key # set to sort using the second element ` ` # lambda list was used ` ` return ` ` (` ` sorted ` ` (tup, key ` ` = ` ` lambda ` ` x: x [` ` 1 ` `])) ` ` `  ` Driver code ` ` tup ` ` = ` ` [(` `` rishav` ` `, ` ` 10 ` `), ( `akash` , 5 ), ( `ram` , 20 ), ( `gaurav` , 15 )] ``    # print a sorted list of tuples print (Sort_Tuple (tup)) `

Exit:

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