sort first element reverse sort second python 1

sort first element reverse sort second python

>>> sorted(lista, key=lambda x: (-x[0], x[1]))
[(485, 'Elizabeth'), (485, 'Matthew'), (390, 'Jayden'), (207, 'Natalie'), (144, 'Chloe'), (51, 'Elijah')]

Here is what the above code is Doing:
1. The sorted() function takes a list as its first argument.
2. The second argument is a key function. This function is applied to each element of the list.
3. The key function returns a value that is used to compare the elements.
4. The key function is called only once for each element of the list.
5. The key function is called before the list is sorted.
6. The key function is called only for sorting purposes.
7. The key function should be pure, which means it should only depend on its arguments and not have any side effects.

Similar Posts