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 :: pandas replace nan with mean 
Python :: remove nans and infs python 
Python :: clone website 
Python :: how to edit messages in discord . py 
Python :: check all values in dictionary python 
Python :: np.random.normal 
Python :: python input timeout 
Python :: get current module name python 
Python :: python regex 
Python :: binary representation python 
Python :: list all files starting with python 
Python :: convert column series to datetime in pandas dataframe 
Python :: python find object with attribute in list 
Python :: reverse range in python 
Python :: remove last element from list python 
Python :: type string python 
Python :: time.time() 
Python :: drop list of columns pandas 
Python :: apply lambda function to multiple columns pandas 
Python :: install python altair 
Python :: how to update list in python 
Python :: python add one 
Python :: tensorflow adam 
Python :: print( n ) in python 
Python :: matplotlib figure cut off 
Python :: get the last element from the list 
Python :: rename pandas columns with list of new names 
Python :: split a string with 2 char each in python 
Python :: filter one dataframe by another 
Python :: python http request params 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =