- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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))
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)]
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.
- 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
- Sort list of tuples by specific ordering in Python
- Update each element in tuple list 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
- Python program to Order Tuples using external List
- Program to sort array by increasing frequency of elements in Python
- How to sort each row of an R data frame in increasing order?