Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python convert a dict to list or a list to dict or a slice a dict or sort a dict by key or value without import

#convert dict to list or list to dict or slice a dict
#1. convert dict to list
temp = [] # not required as [] is mentioned below
s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
temp = [[k,v]for k,v in s.items()]# temp is now list
print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]

#2. list to dict || pass a list like below
a_dict ={}# not required since we have {} in below
a_dict = {  v[0]:v[1] for k,v in enumerate(temp)}
# OR || a_dict = {  v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}

#3. Slice a dict is as simple slice as slicing a list
#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
a_dict = {  v[0]:v[1] for k,v in enumerate(temp[0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}

#Sort a dict #Reverse True for desc and False for asc
#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
print(a_dict)# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#Sort a dict by value || put 1 in x:x[1]
a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}

#5. Sort dict with import operator
import operator as op#change itemgetter to 0|1 for key|value
a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
# refer to https://ideone.com/uufYuP
#don't forget to upvote
Comment

PREVIOUS NEXT
Code Example
Python :: can paypal be hacked by email 
Python :: générer des valeurs de 0 à n python liste 
Python :: Grid-Strategy 
Python :: python chunks 
Python :: how to add base map in pyqgis 
Python :: Solution to Remove Recursion Limitation in python 
Python :: picture as background of seaborn plot python 
Python :: python code for diamond with gap between odd rows 
Python :: condtion for equal time in selenium python 
Python :: python class udp 
Python :: cs50 templating urls 
Python :: logartim normalization python pandas 
Python :: 3x3 gaussian kernel 
Python :: how to upload files and folders with pygithub 
Python :: python modules screen 
Python :: print out python 
Python :: boolean meaning in python 
Python :: Python - Cómo cruda la cuerda 
Python :: off-by-one error in python 
Python :: Allow Complex Number like "1+2j" to be treated as valid number 
Python :: a = np.array([0, 0, 0]) and a = np.array([[0, 0, 0]]) 
Python :: remove cooldown discord python 
Python :: machine learning cheatsheet activation function 
Python :: python extract words from string with format 
Python :: python ufeff character from file 
Python :: check accessability of the file 
Python :: computecost pyspark 
Python :: python file is writable 
Python :: python login to O365 
Python :: numpy np sign change in df pandas zero crossing 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =