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

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 :: how to change the console background color in python 
Python :: python letter to number in alphabet 
Python :: python progress bar 
Python :: list with numbers between 2 values by 
Python :: python tkinter fenstergröße 
Python :: python remove special characters from list 
Python :: python file.write is not writing whole line 
Python :: python group by multiple aggregates 
Python :: check for missing values in pandas 
Python :: smtplib send pdf 
Python :: how to clean environment python 
Python :: how to find the last item of a list 
Python :: splitting a number into digits python 
Python :: object to int and float conversion pandas 
Python :: python read in integers separated by spaces 
Python :: what is hashlib in python 
Python :: python classes 
Python :: check if a the time is 24 hours older python 
Python :: python basic flask app 
Python :: append in a for loop python 
Python :: convert float in datetime python 
Python :: pandas describe kurtosis skewness 
Python :: simple secatter plot 
Python :: django unique together 
Python :: add two list in python 
Python :: change the frequency to column in pandas 
Python :: tensor to int python 
Python :: install SocketIO flask 
Python :: python how to get user input 
Python :: python make comparison non case sensitive 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =