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 program to sort a list of tuples by second Item
- Python | Sort list of tuples by specific ordering
- Python | Remove tuples from list of tuples if greater than n
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Combining tuples in list of tuples
- Sort the words in lexicographical order in Python
- Python | Sort words of sentence in ascending order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.