-3

Hi This might sounds confusing, but I will try to explain best I can. My list of dictionaries looks like this:

{
   "1": {
      "bd_date": "04/02/1977",
      "name": "Deli Mirko",
      "next": "04/02/2021",
      "renew": 1,
      "type": "birthday",
      "until": 1
   },
   "-MScOoqCpbVxSpz56j0B": {
      "bd_date": "25/11/1983",
      "name": "Deli Marina",
      "next": "25/11/2021",
      "renew": 1,
      "type": "birthday",
      "until": 295
   },
   "-MScWwQ6-23Sdd50YoQh": {
      "bd_date": "17/04/1952",
      "name": "Deli Geza",
      "next": "17/04/2021",
      "renew": 1,
      "type": "birthday",
      "until": 73
   }
}

I wish to sort this using "until" values ASC.

Is there a way to do this.

EDIT: I tried suggested solutions, but I don't get result OR it changes my dictionary format. I need to keep format, because the rest of code. I tried

new_list = sorted(old_list.items(), key=lambda i: i[1]['until'])

but it changes format to list from dict - [("x",{...}), ("y",{..})...]

how to change above code to keep format {"x": {...}, "y": {...}}

8
  • 2
    "But I will try to explain best" - you haven't tried to explain much though, but your requirement seems clear. Can you share the code you tried so far? We can help you in fixing your code Commented Feb 3, 2021 at 16:31
  • Does this answer your question? Sort a list of lists with a custom compare function Commented Feb 3, 2021 at 16:32
  • It seems all you need is to use sorted() and specify an appropriate key. Commented Feb 3, 2021 at 16:33
  • You don't have a list of dictionaries. You have a dictionary with dictionary values. What output data structure do you want? Dictionaries are inherently unordered (in most Python versions)? Commented Feb 3, 2021 at 16:41
  • Try: lst = sorted(mydicts.values(), key=lambda v: v['until']). Commented Feb 3, 2021 at 16:56

2 Answers 2

0

You just need to use sorted and specify the sub key you want to sort on like below.

I should also point out that I think you don't mean a Python list in this context though you just mean you have a dictionary of dictionary objects and you want to iterate them:

unsorted_dictionary = {
   "1": {
      "bd_date": "04/02/1977",
      "name": "Deli Mirko",
      "next": "04/02/2021",
      "renew": 1,
      "type": "birthday",
      "until": 1
   },
   "-MScOoqCpbVxSpz56j0B": {
      "bd_date": "25/11/1983",
      "name": "Deli Marina",
      "next": "25/11/2021",
      "renew": 1,
      "type": "birthday",
      "until": 295
   },
   "-MScWwQ6-23Sdd50YoQh": {
      "bd_date": "17/04/1952",
      "name": "Deli Geza",
      "next": "17/04/2021",
      "renew": 1,
      "type": "birthday",
      "until": 73
   }
}

sorted_dictionary = sorted(unsorted_dictionary.items(), key=lambda item: item[1]['until'])

print(sorted_dictionary)
# [('1', {'bd_date': '04/02/1977', 'name': 'Deli Mirko', 'next': '04/02/2021', 'renew': 1, 'type': 'birthday', 'until': 1}), ('-MScWwQ6-23Sdd50YoQh', {'bd_date': '17/04/1952', 'name': 'Deli Geza', 'next': '17/04/2021', 'renew': 1, 'type': 'birthday', 'until': 73}), ('-MScOoqCpbVxSpz56j0B', {'bd_date': '25/11/1983', 'name': 'Deli Marina', 'next': '25/11/2021', 'renew': 1, 'type': 'birthday', 'until': 295})]

for item in sorted_dictionary:
    print(item)
# ('1', {'bd_date': '04/02/1977', 'name': 'Deli Mirko', 'next': '04/02/2021', 'renew': 1, 'type': 'birthday', 'until': 1})
# ('-MScWwQ6-23Sdd50YoQh', {'bd_date': '17/04/1952', 'name': 'Deli Geza', 'next': '17/04/2021', 'renew': 1, 'type': 'birthday', 'until': 73})
# ('-MScOoqCpbVxSpz56j0B', {'bd_date': '25/11/1983', 'name': 'Deli Marina', 'next': '25/11/2021', 'renew': 1, 'type': 'birthday', 'until': 295})
Sign up to request clarification or add additional context in comments.

Comments

-1

Dictionaries are unordered. and that is not a list. That is dictionary of dictionaries. What you can do is You can copy those dictionary items into a list in an orderly fashion.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.