0

I have a list of dicts in python which look like these:

[{'day' : 'Wednesday' , 'workers' : ['John' , 'Smith']} , 
 {'day' : 'Monday' , 'workers' : ['Kelly']}]

I want to sort them by day of week such that the result is

[{'day' : 'Monday' , 'workers' : ['Kelly']},
 {'day' : 'Wednesday' , 'workers' : ['John' , 'Smith']}]

I can use this answer to sort list of just weekday names: Sort week day texts but is there a way to sort the above dict?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Anshul Goyal
  • 119
  • 10
  • 1
    Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – Masklinn Feb 18 '21 at 06:53
  • dicts are unsorted by nature.. why do u need to sort them in the first place.. what are u trying to achieve by sorting the dictionary? – Arun Kaliraja Baskaran Feb 18 '21 at 06:54
  • It's the list that needs to be sorted... – Jiří Baum Feb 18 '21 at 06:54
  • 1
    @ArunKalirajaBaskaran they're trying to sort the list by one of the dict's keys, not trying to sort the dicts themselves. Your statement also has not been true for a while, Python dicts have been following / keeping insertion order since 3.6, and that's part of the language spec since 3.7. – Masklinn Feb 18 '21 at 06:55

4 Answers4

4

The same basic approach that the example you link to uses will work for your list of dictionaries case. The trick is, you need to extract the day value from the dictionaries within the list to make it work. A lambda expression used for the key parameter is one way to do that.

Example:

day_order = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
data = [{'day' : 'Wednesday' , 'workers' : ['John' , 'Smith']} , {'day' : 'Monday' , 'workers' : ['Kelly']}]

sorted(data, key=lambda d: day_order.index(d["day"]))

Output:

[{'day': 'Monday', 'workers': ['Kelly']},
 {'day': 'Wednesday', 'workers': ['John', 'Smith']}]
rhurwitz
  • 2,557
  • 2
  • 10
  • 18
2

Use a lambda function that extracts the weekday name from the dictionary and then returns the index as in your linked question.

weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri"]
list_of_dicts = 
    [{'day' : 'Wednesday' , 'workers' : ['John' , 'Smith']} , 
     {'day' : 'Monday' , 'workers' : ['Kelly']}]
list_of_dicts.sort(key = lambda d: weekdays.index(d['day']))
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

To sort a dictionary by a key "key", you can do:

sorted(dicts, key=lambda d: d["key"])

Merging this with the answer from the question you linked:

m = ["Monday", "Tuesday", ...]
print(sorted(dicts, key=lambda d: m.index(d["day"])))
Roy Cohen
  • 1,540
  • 1
  • 5
  • 22
0

Here it is:

lst = [{'day' : 'Wednesday' , 'workers' : ['John' , 'Smith']} ,
       {'day' : 'Monday' , 'workers' : ['Kelly']},
       {'day' : 'Friday' , 'workers' : ['Nelly']}]

m = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
result = sorted(lst, key= lambda x: m.index(x['day']))

print(result)
#[{'day': 'Monday', 'workers': ['Kelly']}, {'day': 'Wednesday', 'workers': ['John', 'Smith']}, {'day': 'Friday', 'workers': ['Nelly']}]
pakpe
  • 5,391
  • 2
  • 8
  • 23