-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": {...}}

bwanaHA
  • 7
  • 4

2 Answers2

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})
Johnny John Boy
  • 3,009
  • 5
  • 26
  • 50
-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.

SURYA TEJA
  • 204
  • 3
  • 8