The sorted()
function takes a key=
parameter
newlist = sorted(list_to_be_sorted, key=lambda d: d['name'])
Alternatively, you can use operator.itemgetter
instead of defining the function yourself
from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name'))
For completeness, add reverse=True
to sort in descending order
newlist = sorted(list_to_be_sorted, key=itemgetter('name'), reverse=True)
Related Contents:
- How to sort a list of dictionaries by a value of the dictionary in Python?
- In Python, when to use a Dictionary, List or Set?
- Sorting a dictionary with lists as values, according to an element from the list
- How to sort a list of objects based on an attribute of the objects?
- How can I get list of values from dict?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How to keep keys/values in same order as declared?
- Getting a list of values from a list of dicts
- Why does “return list.sort()” return None, not the list? [duplicate]
- Finding median of list in Python
- Create a list with initial capacity in Python
- Python data structure sort list alphabetically
- What would a “frozen dict” be?
- How to sort two lists (which reference each other) in the exact same way
- case-insensitive list sorting, without lowercasing the result?
- From list of integers, get number closest to a given value
- Pythonic way to check if a list is sorted or not
- Check if value already exists within list of dictionaries?
- How to overcome TypeError: unhashable type: ‘list’ [duplicate]
- Does python have a sorted list?
- List of tuples to dictionary [duplicate]
- How to convert list of key-value tuples into dictionary?
- Python – How to sort a list of lists by the fourth element in each list? [duplicate]
- List of dicts to/from dict of lists
- python filter list of dictionaries based on key value
- Appending a dictionary to a list – I see a pointer like behavior
- One liner: creating a dictionary from list with indices as keys
- Convert list into a dictionary [duplicate]
- Python 3 sort a dict by its values
- How do I initialize a dictionary of empty lists in Python?
- Mapping dictionary value to list
- How to sort and remove duplicates from Python list? [duplicate]
- Python Array with String Indices
- What is the underlying data structure for Python lists?
- Sort list while pushing None values to the end
- How do I initialize a dictionary of empty lists in Python?
- django – convert a list back to a queryset [duplicate]
- What is the true difference between a dictionary and a hash table?
- How do I return dictionary keys as a list in Python?
- Sorting list according to corresponding values from a parallel list [duplicate]
- How to make a dictionary that returns key for keys missing from the dictionary instead of raising KeyError?
- python sorting dictionary by length of values
- Removing elements from a list containing specific characters [duplicate]
- How to extract all values from a dictionary in Python?
- Check if value already exists within list of dictionaries in Python?
- How to sort multidimensional array by column?
- Sort a list of numerical strings in ascending order
- Sort nested dictionary by value, and remainder by another value, in Python
- Sorting a dictionary by value then key
- Setting a value in a nested Python dictionary given a list of indices and value