Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

dictionary sort python

d={
  3: 4,
  1: 1,
  0: 0,
  4: 3,
  2: 1
}
y=dict(sorted(d.items(), key=lambda item: item[1]))
print(y) # {0: 0, 2: 1, 1: 2, 4: 3, 3: 4}
Comment

sort dictionary python

l = {1: 40, 2: 60, 3: 50, 4: 30, 5: 20}
d1 = dict(sorted(l.items(),key=lambda x:x[1],reverse=True))
print(d1) #output : {2: 60, 3: 50, 1: 40, 4: 30, 5: 20}
d2 = dict(sorted(l.items(),key=lambda x:x[1],reverse=False))
print(d2) #output : {5: 20, 4: 30, 1: 40, 3: 50, 2: 60}
Comment

python dict order a dict by key

d1 = dict(sorted(d.items(), key = lambda x:x[0]))
Comment

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
Comment

sort the dictionary in python

d = {2: 3, 1: 89, 4: 5, 3: 0}
od = sorted(d.items())
print(od)
Comment

python sort dict by key

A={1:2, -1:4, 4:-20}
{k:A[k] for k in sorted(A)}

output:
{-1: 4, 1: 2, 4: -20}
Comment

python sort dictionary by key

In [1]: import collections

In [2]: d = {2:3, 1:89, 4:5, 3:0}

In [3]: od = collections.OrderedDict(sorted(d.items()))

In [4]: od
Out[4]: OrderedDict([(1, 89), (2, 3), (3, 0), (4, 5)])
Comment

python dictionary sort

# empty dictionary
dictionary = {}
# lists
list_1 = [1, 2, 3, 4, 5]
list_2 = ["e", "d", "c", "b", "a"]
# populate a dictionary.
for key, value in zip(list_1, list_2):
    dictionary[key] = value
# original
print(f"Original dictionary: {dictionary}")

# Sort dictionary based on value
dictionary_sorted = dict(sorted(dictionary.items(), key=lambda value: value[1]))
print(f"Sort dictionary by value: {dictionary_sorted}")

# Sort dictionary based on key
dictionary_sorted = dict(sorted(dictionary.items(), key=lambda key: key[0]))
print(f"Sort dictionary by key: {dictionary_sorted}")
Comment

sort dictionary by value and then key python

sorted(y.items(), key=lambda x: (x[1],x[0]))
Comment

python dict sortieren

# dict has no order - use a list to store
l = {1: 40, 2: 60, 3: 50, 4: 30, 5: 20}
d1 = list(sorted(l.items(),key=lambda x:x[1],reverse=True))
Comment

sort dictionary by key

dictionary_items = a_dictionary.items()
sorted_items = sorted(dictionary_items)
Comment

sort dict of dicts by key

channels = {
'24': {'type': 'plain', 'table_name': 'channel.items.AuctionChannel'}, 
'26': {'type': 'plain', 'table_name': 'channel.gm.DeleteAvatarChannel'}, 
'27': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyChannel'}, 
'20': {'type': 'plain', 'table_name': 'channel.gm.AvatarMoneyAssertChannel'}, 
'21': {'type': 'plain', 'table_name': 'channel.gm.AvatarKillMobComplexChannel'}, 
'22': {'type': 'plain', 'table_name': 'channel.gm.DistributionMarkChannel'}, 
'23': {'type': 'plain', 'table_name': 'channel.gm.MailChannel'}
}

channels = collection.OrderedDict(sorted(channels.items(), key=lambda item: item[0]))
for key,value in channels.items():
    print(key, ':', value)
Comment

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

sort dictionary by key python

for key in sorted(a_dictionary):
        print ("{}: {}".format(key, a_dictionary[key]))
Comment

dict sort

[(k,di[k]) for k in sorted(di.keys())] 
Comment

sorting dictionary in python

Sorting a dictionary in python 
Comment

PREVIOUS NEXT
Code Example
Python :: plt.plot figure size 
Python :: __gt__ 
Python :: dataframe rename column 
Python :: request.body django 
Python :: export_excel file python 
Python :: make directory python 
Python :: python live video streaming flask 
Python :: python defaultdict 
Python :: scikit learn k means 
Python :: python bz2 install 
Python :: python selenium clear input 
Python :: tensorfow list devices 
Python :: word pattern python 
Python :: python data frame check if any nan value present 
Python :: minimum-number-of-steps-to-reduce-number-to-1 
Python :: return max repeated value in list 
Python :: get list of files in directory python 
Python :: localhost server in Python 
Python :: python string math 
Python :: euclidean division in python 
Python :: generate sha1 python 
Python :: replace nat with date pandas 
Python :: python check for folder 
Python :: pandas df row count 
Python :: python list iterate in 1 line 
Python :: dataframe fill none 
Python :: python falsy values 
Python :: prevent list index out of range python 
Python :: how to check which submit button is clicked in flask wtf 
Python :: distinct rows in this DataFrame 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =