how can I sort a dictionary in python according to its values?
s = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
k = dict(sorted(s.items(),key=lambda x:x[0],reverse = True))
print(k)
how can I sort a dictionary in python according to its values?
s = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
k = dict(sorted(s.items(),key=lambda x:x[0],reverse = True))
print(k)
sort dict by values
dict(sorted(x.items(), key=lambda item: item[1]))
sort dictionary
#for dictionary d
sorted(d.items(), key=lambda x: x[1]) #for inceasing order
sorted(d.items(), key=lambda x: x[1], reverse=True) # for decreasing order
#it will return list of key value pair tuples
python sort the values in a dictionaryi
from operator import itemgetter
new_dict = sorted(data.items(), key=itemgetter(1))
python sort dictionary by key
def sort_dict(dictionary, rev = True):
l = list(dictionary.items())
l.sort(reverse = rev)
a = [item[1] for item in l]
z = ''
for x in a:
z = z + str(x)
return(z)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us