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

sorting values in dictionary in python

#instead of using python inbuilt function we can it compute directly.
#here iam sorting the values in descending order..
d = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
s=[]
for i in d.items():
  s.append(i)
for i in range(0,len(s)):
  for j in range(i+1,len(s)):
    if s[i][1]<s[j][1]:
      s[i],s[j]=s[j],s[i]
print(dict(s))
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 :: geopandas stack or concatenate dataframe together 
Python :: how to dump a database using manage.py 
Python :: count consecutive values in python 
Python :: django-sslserver 
Python :: how to concatenate a string with int in python 
Python :: flatten tf keras 
Python :: Matplotlib rotated xticklabels 
Python :: django form list option 
Python :: convert all colnames of dataframe to upper 
Python :: power function python 
Python :: extract nonzero array elements python 
Python :: python concatenate lists 
Python :: how to get the link of an image in selenium python 
Python :: Python NumPy copyto function example 
Python :: how to get last n elements of a list in python 
Python :: find all regex matches python 
Python :: python split lines 
Python :: python get desktop environment 
Python :: how to extract domain name from url python 
Python :: how to get input from pyqt line edit 
Python :: How to Use Python all() Function to Check for Letters in a String using all function 
Python :: how to find maximum number from python list 
Python :: how to change index in dataframe python 
Python :: upload to test pypi 
Python :: generate random integers 
Python :: how to use a function to find the average in python 
Python :: extract all text from website using beautifulsoup and python 
Python :: python if 
Python :: python print last 3 
Python :: python datetime add 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =