Given a list of tuples, write a Python program to sort the tuples by the second item of each tuple.

**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 the Bubble Sort

Using the technique of Bubble Sort to we can perform the sorting. Note that each tuple is an element in the given list. Access the second element of each tuple using the nested loops. This performs the in-place method of sorting. The time complexity is similar to the Bubble Sort i.e. O(n^2).

`# Python program to sort a list of tuples by the second Item ` ` ` `# Function to sort the list of tuples by its second item ` `def` `Sort_Tuple(tup): ` ` ` ` ` `# getting length of list of tuples ` ` ` `lst ` `=` `len` `(tup) ` ` ` `for` `i ` `in` `range` `(` `0` `, lst): ` ` ` ` ` `for` `j ` `in` `range` `(` `0` `, lst` `-` `i` `-` `1` `): ` ` ` `if` `(tup[j][` `1` `] > tup[j ` `+` `1` `][` `1` `]): ` ` ` `temp ` `=` `tup[j] ` ` ` `tup[j]` `=` `tup[j ` `+` `1` `] ` ` ` `tup[j ` `+` `1` `]` `=` `temp ` ` ` `return` `tup ` ` ` `# Driver Code ` `tup ` `=` `[(` `'for'` `, ` `24` `), (` `'is'` `, ` `10` `), (` `'Geeks'` `, ` `28` `), ` ` ` `(` `'Geeksforgeeks'` `, ` `5` `), (` `'portal'` `, ` `20` `), (` `'a'` `, ` `15` `)] ` ` ` `print` `(Sort_Tuple(tup)) ` |

*chevron_right*

*filter_none*

**Output:**

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

**Method #2:** Using sort() method

While sorting via this method the actual content of the tuple is changed, and just like the previous method, the in-place method of the sort is performed.

`# Python program to sort a list of ` `# tuples by the second Item using sort() ` ` ` `# Function to sort hte list by second item of tuple ` `def` `Sort_Tuple(tup): ` ` ` ` ` `# reverse = None (Sorts in Ascending order) ` ` ` `# key is set to sort using second element of ` ` ` `# sublist lambda has been used ` ` ` `tup.sort(key ` `=` `lambda` `x: x[` `1` `]) ` ` ` `return` `tup ` ` ` `# Driver Code ` `tup ` `=` `[(` `'rishav'` `, ` `10` `), (` `'akash'` `, ` `5` `), (` `'ram'` `, ` `20` `), (` `'gaurav'` `, ` `15` `)] ` ` ` `# printing the sorted list of tuples ` `print` `(Sort_Tuple(tup)) ` |

*chevron_right*

*filter_none*

**Output:**

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

**Method #3:** Using sorted() method

Sorted() method sorts a list and always returns a list with the elements in a sorted manner, without modifying the original sequence. It takes three parameters from which two are optional, here we tried to use all of the 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 would serve as a key or a basis of sort comparison.

**Reverse(optional) :** To sort this in ascending order we could have just ignored the third parameter, which we did in this program. If set true, then the iterable would be sorted in reverse (descending) order, by default it is set as false.

`# Python program to sort a list of ` `# tuples by the second Item using sorted() ` ` ` `# Function to sort the list by second item of tuple ` `def` `Sort_Tuple(tup): ` ` ` ` ` `# reverse = None (Sorts in Ascending order) ` ` ` `# key is set to sort using second element of ` ` ` `# sublist lambda has been used ` ` ` `return` `(` `sorted` `(tup, key ` `=` `lambda` `x: x[` `1` `])) ` ` ` `# Driver Code ` `tup ` `=` `[(` `'rishav'` `, ` `10` `), (` `'akash'` `, ` `5` `), (` `'ram'` `, ` `20` `), (` `'gaurav'` `, ` `15` `)] ` ` ` `# printing the sorted list of tuples ` `print` `(Sort_Tuple(tup)) ` |

*chevron_right*

*filter_none*

**Output:**

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

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert string tuples to list tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python | Group by matching second tuple value in list of tuples
- Python | How to Concatenate tuples to nested tuples
- Python program to sort a list of tuples alphabetically
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Python | Largest, Smallest, Second Largest, Second Smallest in a List
- Python | Convert list of tuples to list of list
- Sort the values of first list using second list in Python
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python - Sort list of Single Item dictionaries according to custom ordering
- Python | Sort list of tuples by specific ordering

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.