Given a tuple, sort the list of tuples in increasing order by any key in tuple.
Input : tuple = [(2, 5), (1, 2), (4, 4), (2, 3)] m = 0 Output : [(1, 2), (2, 3), (2, 5), (4, 4)] Explanation: Sorted using the 0th index key. Input : [(23, 45, 20), (25, 44, 39), (89, 40, 23)] m = 2 Output : Sorted: [(23, 45, 20), (89, 40, 23), (25, 44, 39)] Explanation: Sorted using the 2nd index key
Given tuples, we need to sort them according to any given key. This can be done using sorted() function where we sort them using key=last and store last as the key index according to which we have to sort the given tuples.
Below is the Python implementation of the above approach:
Sorted: [(23, 45, 20), (89, 40, 23), (25, 44, 39)]
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Sort a Matrix in all way increasing order
- Sort even and odd placed elements in increasing order
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Sort only non-prime numbers of an array in increasing order
- Python | Extract unique tuples from list, Order Irrespective
- Python | Remove duplicate lists in tuples (Preserving Order)
- Python program to sort a list of tuples alphabetically
- Python | Sort list of tuples by specific ordering
- Python program to sort a list of tuples by second Item
- Python | Remove duplicate tuples from 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 | Convert string tuples to list tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.