Answers for "sort based on key python"

0

list sort by key python

>>> student_tuples = [
...     ('john', 'A', 15),
...     ('jane', 'B', 12),
...     ('dave', 'B', 10),
... ]
>>> sorted(student_tuples, key=lambda student: student[2])   
# sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Posted by: Guest on February-01-2021
1

sort key python

>>> sorted(student_tuples, key=itemgetter(2))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Posted by: Guest on July-21-2021
1

python sort dictionary by key

def sort_dict(dictionary, rev = True):
    l = list(dictionary.items())
    l.sort(reverse = rev)
    a = [item[1] for item in l]    
    z = ''    
    for x in a:
        z = z + str(x)
    return(z)
Posted by: Guest on July-22-2021
1

sort key python

>>> class Student:
...     def __init__(self, name, grade, age):
...         self.name = name
...         self.grade = grade
...         self.age = age
...     def __repr__(self):
...         return repr((self.name, self.grade, self.age))
Posted by: Guest on July-21-2021

Code answers related to "sort based on key python"

Python Answers by Framework

Browse Popular Code Answers by Language