Sometimes, while participating in a competitive programming test, we can be encountered with a problem in which we require to sort a pair in opposite orders by indices. This particular article focuses on solving a problem in which we require to sort the number in descending order and then the String in increasing order. This is the type of problem which is common in the sorting of pairs. Let’s discuss a way in which this can be solved.
Method : Using
sorted() + lambda
The combination of these functions can be used to perform this task. In these, we pass to lambda function the negative of the values so that the increasing order of number is evaluated as decreasing and hence a successful hack to perform this task.
The original list is : [(‘Geeks’, 5), (‘For’, 3), (‘Geeks’, 6), (‘Is’, 5), (‘Best’, 7), (‘For’, 5), (‘CS’, 3)]
The list after inverse sorted tuple elements : [(‘Best’, 7), (‘Geeks’, 6), (‘For’, 5), (‘Geeks’, 5), (‘Is’, 5), (‘CS’, 3), (‘For’, 3)]
- Python | Sorting string using order defined by another string
- Inverse Gamma Distribution in Python
- Python | Inverse Number Theoretic Transformation
- Python | Inverse Fast Fourier Transformation
- Python | Inverse Fast Walsh Hadamard Transformation
- Python | Integrity Sorting in two lists
- Python | Custom sorting in list of tuples
- Python | Sorting URL on basis of Top Level Domain
- Python | Sorting list of lists with similar list elements
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
- String slicing in Python to rotate a string
- Python | Add one string to another
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.