Interesting. I hadn't realized insertion order was formalized, though I've been using Python since two point something and sort of had noticed the change in behavior. Nice to know it's a property I can count on.
I have a project I'm working on where I don't just need insertion order of the keys, I also need the index position of the keys. At first I thought that was something that OrderedDict was going to give me, but it doesn't have any methods for anything like that, so now I'm building my own dictionary class IndexedDict, that has an internal list to track the key position.
Interesting. I hadn't realized insertion order was formalized, though I've been using Python since two point something and sort of had noticed the change in behavior. Nice to know it's a property I can count on.
It makes standard dictionaries useable in more use cases, and there’s no drawback. Win-win
Short and highly informative 👏 I think quick articles like this interspersed among the longer ones are a nice touch Stephen!
I'm planning to mix-and-match more between short and long, "traditional" and "narrative", and so on…
The next one is also short, and a follow up to this one (writing it now)
I have a project I'm working on where I don't just need insertion order of the keys, I also need the index position of the keys. At first I thought that was something that OrderedDict was going to give me, but it doesn't have any methods for anything like that, so now I'm building my own dictionary class IndexedDict, that has an internal list to track the key position.
You could presumably build upon `OrderedDict` and use `list(self.keys())` to get a sequence of keys. Is that the route you’re taking?