Given, a list of tuple, the task is to sort the list of tuples based on the sum of elements in the tuple.

**Examples:**

Input:[(4, 5), (2, 3), (6, 7), (2, 8)]

Output:[(2, 3), (4, 5), (2, 8), (6, 7)]

Input:[(3, 4), (7, 8), (6, 5)]

Output:[(3, 4), (6, 5), (7, 8)]

**# Method 1:** Using 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 elements 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 code to sort list of tuple based on sum of element in tuple. ` ` ` `# Input list initialisation ` `Input` `=` `[(` `4` `, ` `5` `), (` `2` `, ` `3` `), (` `6` `, ` `7` `), (` `2` `, ` `8` `)] ` ` ` `print` `(` `"The original list of tuple is "` `) ` `print` `(` `Input` `) ` ` ` `# getting length of list of tuples ` `lst ` `=` `len` `(` `Input` `) ` ` ` `# Bubble sort ` `for` `i ` `in` `range` `(lst): ` ` ` ` ` `for` `j ` `in` `range` `(lst` `-` `i` `-` `1` `): ` ` ` `if` `(` `Input` `[j][` `0` `]` `+` `Input` `[j][` `1` `]) > (` `Input` `[j` `+` `1` `][` `0` `]` `+` `Input` `[j` `+` `1` `][` `1` `]): ` ` ` `Input` `[j], ` `Input` `[j` `+` `1` `] ` `=` `Input` `[j` `+` `1` `], ` `Input` `[j] ` ` ` ` ` `# print output ` `print` `(` `"\nThe answer is"` `) ` `print` `(` `Input` `) ` |

*chevron_right*

*filter_none*

**Output:**

The original list of tuple is [(4, 5), (2, 3), (6, 7), (2, 8)] The answer is [(2, 3), (4, 5), (2, 8), (6, 7)]

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

This is the most basic efficient and short method to achieve the solution to this task.

In this, we pass lambda as key to the list of tuples.

`# Python code to sort list of tuple based on sum of element in tuple. ` ` ` `# Input list initialisation ` `Input` `=` `[(` `4` `, ` `5` `), (` `2` `, ` `3` `), (` `6` `, ` `7` `), (` `2` `, ` `8` `)] ` ` ` `print` `(` `"The original list of tuple is "` `) ` `print` `(` `Input` `) ` ` ` `# Passing lambda as key to sort list of tuple ` `print` `(` `"\nThe answer is"` `) ` `print` `(` `sorted` `(` `Input` `, key ` `=` `lambda` `x:x[` `0` `] ` `+` `x[` `1` `])) ` |

*chevron_right*

*filter_none*

**Output: **

The original list of tuple is [(4, 5), (2, 3), (6, 7), (2, 8)] The answer is [(2, 3), (4, 5), (2, 8), (6, 7)]

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

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

`# Python code to sort list of tuple based on sum of element in tuple. ` ` ` `# Input list initialisation ` `Input` `=` `[(` `4` `, ` `5` `), (` `2` `, ` `3` `), (` `6` `, ` `7` `), (` `2` `, ` `8` `)] ` ` ` `print` `(` `"The original list of tuple is "` `) ` `print` `(` `Input` `) ` ` ` `# Passing lambda as key to sort list of tuple ` `Input` `.sort(key ` `=` `lambda` `x: x[` `0` `] ` `+` `x[` `1` `]) ` ` ` `# Printing output ` `print` `(` `"\nThe answer is"` `) ` `print` `(` `Input` `) ` |

*chevron_right*

*filter_none*

**Output:**

The original list of tuple is [(4, 5), (2, 3), (6, 7), (2, 8)] The answer is [(2, 3), (4, 5), (2, 8), (6, 7)]

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 | Sort tuple list by Nth element of tuple
- Python | Sort tuple based on occurrence of first element
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python | Remove duplicates based on Kth element tuple list
- Python | Group tuple into list based on value
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python program to convert Set into Tuple and Tuple into Set
- Python | Sort tuple list on basis of difference of elements
- Python - Sort by Frequency of second element in Tuple List
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Python program to sort a dictionary list based on the maximum value
- Python program to sort matrix based upon sum of rows
- Python | Pair and combine nested list to tuple list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list to indexed tuple list

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.