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

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

sort list of list of dictionaries python

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

PREVIOUS NEXT
Code Example
Python :: python / vs // 
Python :: join python 
Python :: sort 2d list python 
Python :: json.dump 
Python :: python string: .find() 
Python :: start and end index in python 
Python :: how to find duplicates in pandas 
Python :: what are tuples in python 
Python :: pandas sample 
Python :: python how to put int into list 
Python :: math in python 
Python :: if statements python 
Python :: list unpacking python 
Python :: python return 
Python :: formatting strings in python 
Python :: create a range of numbers in python 
Python :: how to check list is empty or not 
Python :: getting url parameters with javascript 
Python :: IndexError: list assignment index out of range 
Python :: how to replace special characters in a string python 
Python :: self.variable 
Python :: iterate python 
Python :: python if elif else syntax 
Python :: thresholding with OpenCV 
Python :: df.info() in python 
Python :: add column python list 
Python :: python loop over list 
Python :: pandas previous row 
Python :: Python NumPy tile Function Syntax 
Python :: robot framework log from python 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =