Sort list elements in a specified order in Python

A note when you want to sort the elements of a list in a specified order in Python.

I couldn't find a way to sort by another list by searching, so I wrote it. It can be used when you want to sort the response of external API or DB.

sort.py


from pprint import pprint

data_list = [
    {"id": 1, "name": "aaa"},
    {"id": 2, "name": "bbb"},
    {"id": 3, "name": "ccc"},
    {"id": 4, "name": "ddd"},
    {"id": 5, "name": "eee"},
]
order = [
    1,
    5,
    3,
    2,
    4
]

pprint(data_list)
print "-----"
pprint(sorted(data_list, key=lambda data: order.index(data["id"])))

result


[{'id': 1, 'name': 'aaa'},
 {'id': 2, 'name': 'bbb'},
 {'id': 3, 'name': 'ccc'},
 {'id': 4, 'name': 'ddd'},
 {'id': 5, 'name': 'eee'}]
-----
[{'id': 1, 'name': 'aaa'},
 {'id': 5, 'name': 'eee'},
 {'id': 3, 'name': 'ccc'},
 {'id': 2, 'name': 'bbb'},
 {'id': 4, 'name': 'ddd'}]

Recommended Posts

Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
Getting list elements in Python
Group by consecutive elements of a list in Python
[python] Manage functions in a list
Delete multiple elements in python list
Sort and output the elements in the list as elements and multiples in Python.
Get the number of specific elements in a python list
Display a list of alphabets in Python 3
How to delete multiple specified positions (indexes) in a Python list
Sorted list in Python
[Python] Sort the list of pathlib.Path in natural sort
How to clear tuples in a list (Python)
Bubble sort in Python
Make a copy of the list in Python
A memo about writing merge sort in Python
Rewriting elements in a loop of lists (Python)
I want to sort a list in the order of other lists
View drug reviews using a list in Python
List find in Python
Custom sort in Python3
Randomly select elements from list (array) in python
Natural order in python
How to remove duplicate elements in Python3 list
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
[Python] Manipulation of elements in list (array) [Add / Delete]
Things to note when initializing a list in Python
[Python] How to sort dict in list and instance in list
[Python] Outputs all combinations of elements in the list
[Python] How to output the list values in order
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
Naturally sort Path in Python
python in mongodb in descending sort
Python list is not a list
Make a bookmarklet in Python
Sort by date in python
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
How to check in Python if one of the elements of a list is in another list
Make sure all the elements in the list are the same in Python
Create a list in Python with all followers on twitter
[Python] Create a date and time list for a specified period
Developed a library to get Kindle collection list in Python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Create a new list by combining duplicate elements in the list
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
Get the value of a specific key up to the specified index in the dictionary list in Python
Extract multiple list duplicates in Python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
Difference between list () and [] in Python
Hit a command in Python (Windows)
Create a DI Container in Python
Draw a scatterplot matrix in python
Write A * (A-star) algorithm in Python
Create a binary file in Python
Type specified in python. Throw exceptions
Sort large text files in Python
Solve ABC036 A ~ C in Python