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 list of dictionaries by key python

newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 
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

sort list of dictionaries python

unsorted_list = [{"key1":5, "key2":2}, {"key1":5, "key2":1}]
sorted_list = sorted(unsorted_list, key=lambda k: k["key2"])
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

how to sort list of dictionaries in python

rows = [
{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]

from operator import itemgetter
rows_by_fname = sorted(rows, key= itemgetter('fname'))
rows_by_uid = sorted(rows, key=itemgetter('uid'))
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

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

sorting dictionary in python

Sorting a dictionary in python 
Comment

PREVIOUS NEXT
Code Example
Python :: python reversed range 
Python :: python replace string 
Python :: how to install python 3.6.0 on debian 
Python :: delete columns pandas 
Python :: dropna in specific column pandas 
Python :: np array to tuple 
Python :: streamlit python install 
Python :: how to convert to string in python 
Python :: print class python 
Python :: kafka get last offset of topic python 
Python :: python substitute multiple letters 
Python :: number of words in a string python 
Python :: from math import python 
Python :: python package version 
Python :: login_required on class django 
Python :: Extract column from a pandas dataframe 
Python :: drop every other column pandas 
Python :: python remove first element from list 
Python :: pandas max columns 
Python :: tkinter get child in frame 
Python :: How to install pandas-profiling 
Python :: custom validation in django models 
Python :: apply same shuffle to two arrays numpy 
Python :: pandas change order of columns in multiindex 
Python :: python-telegram-bot 
Python :: with in python 
Python :: python find in list 
Python :: multiline comment python 
Python :: seaborn boxplot 
Python :: how to compile python 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =