Search
 
SCRIPT & CODE EXAMPLE
 

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

PREVIOUS NEXT
Code Example
Python :: Django custome login 
Python :: create columns in streamlit 
Python :: how can i plot graph from 2 dataframes in same window python 
Python :: how to find the closest value in column python 
Python :: random int python 
Python :: how to execute a python file from another python file 
Python :: end python print with space 
Python :: download from colab to local drive 
Python :: sort a dictionary 
Python :: python arguments 
Python :: Calculate Euclidean Distance in Python using norm() 
Python :: python print string name in pattern 
Python :: filter django or 
Python :: xlabel and ylabel in python 
Python :: set xlim histogram python 
Python :: app is not a registered namespace django 
Python :: np.multiply 
Python :: string remove in python 
Python :: How to append train and Test dataset in python 
Python :: python datetime 
Python :: how to dump a database using manage.py 
Python :: pip install django celery results 
Python :: python keyboard key codes 
Python :: match statement 
Python :: print from within funciton with multiprocessing 
Python :: capitalize first letter of each word python 
Python :: np.select with multiple conditions 
Python :: how to print correlation to a feature in pyhton 
Python :: python is dict 
Python :: python program to check if binary representation is a palindrome 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =