Search code examples
pythonpython-3.7

Elements in defaultdict change order


I have a simple loop, that appends new items to a defaultdict(list):

import spacy
from collections import defaultdict
import json
from pprint import pprint

def run():
   nlp = spacy.load('en_core_web_sm')
   doc = nlp(sentence)
   sentence = "Hi my name is Oliver!"
   ners = defaultdict(list)
   ners['text'] = str(sentence)

   #Simply loop:
   for ent in doc.ents:
       ners['extractions'].append({
            "label": str(ent.label_),
            "text": str(ent.text),
            "confidence": round(score, 2),
            "start_position": ent.start_char,
            "end_position": ent.end_char
       })

   #Print out the defaultdict.
   pprint(ners)

The above prints out:

{
   "extractions":[
      {
         "confidence":1.0,
         "end_position":20,
         "label":"PERSON",
         "start_position":14,
         "text":"Oliver"
      }
   ],
   "text":"Hi my name is Oliver"
})

As you can see, the order of the keys is not the same as in the loop (for example, end_position and start_position have changed places)

How can I keep the same output order, as the one I am writing in the code?

I am running Python 3.7.3.


Solution

  • As said by martineau, pprint() sorts the keys by default when printing.

    In Python 3.8+ you can add the sort_dicts=False to disable this:

    import pprint
    pprint(something, sort_dicts=False)