[Tutor] Preserve Ordering in the Dict

jark AJ jarkmx at gmail.com
Tue May 4 00:30:11 EDT 2021


Hi All,

       I am using Python 2.7.
I have a dictionary in the following format and when I print it, I get the
result not in the same order as shown. I am looking to preserve the order.
I looked at the ordered dict and sorted, however not able get the desired
result. Could you please help if there is a way we can achieve this, thanks
in advance!!

data = {
          "name": "my_name",
          "items": [
              {"type": "type_name",
               "details": [
                   {
                       "key": "key_name",
                       "value": "value_name"
                   }
               ]
               }
          ]
      }

print(data)


Current Result:

{'items': [{'type': 'type_name', 'details': [{'value': 'value_name', 'key':
'key_name'}]}], 'name': 'my_name'}

Desired Result:

{'name': 'my_name', 'items': [{'type': 'type_name', 'details': [{'key':
'key_name', 'value': 'value_name'}]}]}


More information about the Tutor mailing list