
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
When it is required sort a list of tuples in the increasing order, based on the last element of every tuple in the list of tuple, methods are defined to first sort the list of tuple based on last element, and another method to sort based on previous method.
Below is a demonstration of the same −
Example
def sort_last_elem(num): return num[-1] def sort_structure(my_tuple): return sorted(my_tuple, key=sort_last_elem) my_list = [(45, 31), (23, 67), (92, 60), (90, 12)] print("The list is :") print(my_list) print("The sorted list of elements is :") print(sort_structure(my_list))
Output
The list is : [(45, 31), (23, 67), (92, 60), (90, 12)] The sorted list of elements is : [(90, 12), (45, 31), (92, 60), (23, 67)]
Explanation
A method named ‘sort_last_elem’ is defined that sorts the list based on the last element.
Another method named ‘sort_structure’ is defined that returns the sorted list, based on the key which is the previously defined function.
A list of tuple is defined, and is displayed on the console.
The method is called by passing this list of tuple.
It is displayed as the output on the console.
- Related Articles
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- Python program to sort tuples in increasing order by any key.
- Sort Tuples in Increasing Order by any key in Python program
- Reverse each tuple in a list of tuples in Python
- Python program to sort a list of tuples by second Item
- Python program to Sort Tuples by their Maximum element
- Python program to sort a tuple by its float element
- Python program to sort a list of tuples alphabetically
- Python Program to Sort A List Of Names By Last Name
- Sort list of tuples by specific ordering in Python
- Python program to create a list of tuples from the given list having the number and its cube in each tuple
- Python Group by matching second tuple value in list of tuples
- Update each element in tuple list in Python
- Python program to Order Tuples using external List
- Python Program to Sort a Tuple By Values

Advertisements