Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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 the dictionary in python

d = {2: 3, 1: 89, 4: 5, 3: 0}
od = sorted(d.items())
print(od)
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

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

sorting dictionary in python

Sorting a dictionary in python 
Comment

sort list of list of dictionaries python

sort_list = sorted(list_cities, key=lambda k: k[-1]['distance'])
Comment

PREVIOUS NEXT
Code Example
Python :: how to write a numpy array to a file in python 
Python :: python sum of natural numbers recursion 
Python :: python write to file csv 
Python :: python csv read header only 
Python :: python print int in string with zero padding 
Python :: how to reverse a list in python 
Python :: python voice recognition 
Python :: win32api.mouse_event python 
Python :: import serial python 
Python :: how to import iris dataset 
Python :: connect flask with postgresql 
Python :: Basic method of Converting List to Dataframe 
Python :: max of matrix numpy 
Python :: remove empty rows csv python 
Python :: python random choice int 
Python :: python isprime 
Python :: python pip install 
Python :: set dtype for multiple columns pandas 
Python :: create a list of characters python 
Python :: how to use prettytable with python 
Python :: pandas strips spaces in dataframe 
Python :: Example XlsxWriter in Python 
Python :: python find closest value in list 
Python :: python flask mail 
Python :: python append element to array 
Python :: python __gt__ 
Python :: star operator python 
Python :: initialize an array in python 
Python :: flask redirect to url 
Python :: adding numbers using python function 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =