from operator import itemgetter
grade = [(‘Freddy’, ‘Frank’, 3), (‘Anil’, ‘Frank’, 100), (‘Anil’, ‘Wang’, 24)]
sorted(grade, key=itemgetter(1,0))
sorted(grade, key=itemgetter(0,-1))
from operator import itemgetter
grade = [(‘Freddy’, ‘Frank’, 3), (‘Anil’, ‘Frank’, 100), (‘Anil’, ‘Wang’, 24)]
sorted(grade, key=itemgetter(1,0))
sorted(grade, key=itemgetter(0,-1))
What is this?
For usage of
operator.itemgetter(item )
operator.itemgetter(*items )
Scroll through the operator docs until the section operator.itemgetter
Operator
how do I sort a list using itemgetter
Review the example on link below:
Ways to sort list of dictionaries by values in Python – Using itemgetter
You mention sorted rather then sort. But may be sort is of interest either
How do operator.itemgetter() and sort() work?
Sorted
The sorted()
function in Python is used to sort an iterable in ascending order and returns a new sorted list. The list.sort()
method, on the other hand, sorts the list in-place and returns None
@MariaBu One may get direct links to methods by hovering over the line and clicking the pilchow ¶
at the end of the line.
https://docs.python.org/3/library/operator.html#operator.itemgetter
Do you have a question about the code, or are you just trying to showcase something?
Can you explain itemgetter(0,-1), is it taking elements by index -1,-2,-3 or by alphabet Anil, Freddy.