Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

list sort by key and value

# dictionary sort by key and value
d = { ' a ' : 4, ' b ' : 3, ' c ' : 2, ' d ' : 1 }
sort_by_key= sorted(d.items(), key = lambda x : x[0] )	    # [(' a ', 4), (' b ', 3), (' c ', 2), (' d ', 1)]
sort_by_value= sorted(d.items(), key = lambda x : x[1] )	# [(' d ', 1), (' c ', 2), (' b ', 3), (' a ', 4)]
print(sort_by_key)
print(sort_by_value)
 
PREVIOUS NEXT
Tagged: #list #sort #key
ADD COMMENT
Topic
Name
2+3 =