Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)]
Comment

sort key python

>>> sorted(student_tuples, key=itemgetter(2))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Comment

sort key python

>>> student_objects = [
...     Student('john', 'A', 15),
...     Student('jane', 'B', 12),
...     Student('dave', 'B', 10),
... ]
>>> sorted(student_objects, key=lambda student: student.age)   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Comment

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))
Comment

PREVIOUS NEXT
Code Example
Python :: kivy video recorder 
Python :: python http handler iis 
Python :: what does alpha in plt.txt do 
Python :: Value Error handling 
Python :: http response template 
Python :: numpy prod 
Python :: advanced use of tuples in python 
Python :: how to run django server outside world 
Python :: snap pdf 
Python :: branchless if python 
Python :: return positon of ele in list python 
Python :: /bin/sh: 1: python: not found code runner 
Python :: load python 
Python :: pandas apply dont convert to timestamp 
Python :: A Simple Class 
Python :: python read text on screen 
Python :: how to create sets in python 
Python :: best python library to download files 
Python :: Python Tkinter MenuButton Widget Syntax 
Python :: Comparing Sets with isdisjoint() Function in python 
Python :: pythonanywhere api 
Python :: Joining String And Variable 
Python :: python show difference between two strings and colorize it 
Python :: python pod status phase 
Python :: is tkinter built into python 
Python :: list comperhension condition in python 
Python :: python using recursion advanced 
Python :: how to downlaod file using python 
Python :: get random bright hex color python 
Python :: in np array how to make element as 1 if it exceeds the threshold 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =