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 list by key

# sort list by key
st1 = [ [ 1, 2], [3, 4], [4, 2], [ -1, 3], [ 4, 5], [2, 3]]
st1.sort()
print(st1)					# sort by  1st element [ [ -1, 3 ], [ 1, 2 ], [ 2, 3 ] ...
st1.sort(reverse=True)
print(st1)					# sort decending [ [ 4, 5 ], [ 3, 4], [ 2, 3 ] ...
st1.sort(key=lambda x: x[1])			# sort by 2nd element accending
print(st1)					# [ [ 1, 2 ], [ 4, 2 ], [ -1, 3 ] ...
st1.sort(key=lambda x: x[0] + x[1])		# sort by sum of 1st and 2nd element accending
print(st1)					# [ [ -1, 3 ], [ 1, 2 ], [ 2, 3 ] ...
# lambda undefined function
def sort_func(x) :				# classic way
	return x[0] + x[1]
st1.sort(key=sort_func)
st1.sort(key=lambda x: x[0] + x[1])		# defined function lambda at place

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

python sort array by key

# sort list by key
st1 = [ [ 1, 2], [3, 4], [4, 2], [ -1, 3], [ 4, 5], [2, 3]]
st1.sort()
print(st1)					# sort by  1st element [ [ -1, 3 ], [ 1, 2 ], [ 2, 3 ] ...
st1.sort(reverse=True)
print(st1)					# sort decending [ [ 4, 5 ], [ 3, 4], [ 2, 3 ] ...
st1.sort(key=lambda x: x[1])			# sort by 2nd element accending
print(st1)					# [ [ 1, 2 ], [ 4, 2 ], [ -1, 3 ] ...
st1.sort(key=lambda x: x[0] + x[1])		# sort by sum of 1st and 2nd element accending
print(st1)					# [ [ -1, 3 ], [ 1, 2 ], [ 2, 3 ] ...
# lambda undefined function
def sort_func(x) :				# classic way
	return x[0] + x[1]
st1.sort(key=sort_func)
st1.sort(key=lambda x: x[0] + x[1])		# defined function lambda at place
Comment

PREVIOUS NEXT
Code Example
Python :: boto3 read excel file from s3 into pandas 
Python :: how to use the print function in python 
Python :: turtle example in python 
Python :: divide a column value in pandas dataframe 
Python :: how to read numbers from a text file in python 
Python :: python ssl module is not available 
Python :: read excel file in python 
Python :: python resize image in tkinter 
Python :: ValueError: `logits` and `labels` must have the same shape, received ((None, 2) vs (None, 1)). 
Python :: how to create a list in python 
Python :: json to base64 python 
Python :: merge two Python dictionaries in a single expression 
Python :: convert float to integer pandas 
Python :: python check if all caps 
Python :: np vstack 
Python :: python how to convert csv to array 
Python :: how to open an image in opencv 
Python :: Read all the lines as a list in a file using the readlines() function 
Python :: how to round off values in columns in pandas in excel 
Python :: python write list to excel file 
Python :: python zeros to nan 
Python :: pyspark show all values 
Python :: convert array to set python 
Python :: rotate 90 degrees clockwise counter python 
Python :: subprocess.check_output python 
Python :: square all elements in list python 
Python :: is there a way to skip the first loop on a for loop python 
Python :: sort series in ascending order 
Python :: python docstring example 
Python :: how to add two matrix using function in python 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =