DekGenius.com
PYTHON
sort list of dictionaries by key python
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
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}
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"])
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}
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)])
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'))
Sorting a List of Dictionaries
csv_mapping_list = [
{ "Name": "Jeremy", "Age": 25, "Favorite Color": "Blue" },
{ "Name": "Ally", "Age": 41, "Favorite Color": "Magenta" },
{ "Name": "Jasmine", "Age": 29, "Favorite Color": "Aqua" }
]
# Custom sorting
size = len(csv_mapping_list)
for i in range(size):
min_index = i
for j in range(i + 1, size):
if csv_mapping_list[min_index]["Age"] > csv_mapping_list[j]["Age"]:
min_index = j
csv_mapping_list[i], csv_mapping_list[min_index] = csv_mapping_list[min_index], csv_mapping_list[i]
# List sorting function
csv_mapping_list.sort(key=lambda item: item.get("Age"))
# List sorting using itemgetter
from operator import itemgetter
f = itemgetter('Name')
csv_mapping_list.sort(key=f)
# Iterable sorted function
csv_mapping_list = sorted(csv_mapping_list, key=lambda item: item.get("Age"))
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)
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)
sort dictionary by key python
for key in sorted(a_dictionary):
print ("{}: {}".format(key, a_dictionary[key]))
sorting dictionary in python
Sorting a dictionary in python
sort list of list of dictionaries python
sort_list = sorted(list_cities, key=lambda k: k[-1]['distance'])
© 2022 Copyright:
DekGenius.com