I've got a list of dictionaries like this:
users = [{'id':1, 'name': 'shreyans'}, {'id':2, 'name':'alex'}, {'id':3, 'name':'david'}]
and a list of ids with the desired order:
order = [3,1,2]
What's the best way to order the list users
by the list order
?
users = [{'id':1, 'name': 'shreyans'},
{'id':2, 'name':'alex'},
{'id':3, 'name':'david'}]
order = [3,1,2]
users.sort(key=lambda x: order.index(x['id']))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With