Hacking The Fender "Dictionary" question

Hi! i have question - is “Ordered dictionary” the same to “Dictionary”? in the video i see a regular dictionary instead of what i have, check out screenshot please.

1 Like

Not quite, no.

OrderedDict is a unique object of its own. It’s not as useful nowadays, because since Python 3.7 the regular dict object exhibits some of the behaviour that the OrderedDict was created to address - like remembering the order in which items are added to the dictionary.

You can see the differences between OrderedDict and dict in the Python docs, here.

I suspect that the reason you’re seeing something different to the video, is because the behaviour of csv.DictReader() changed in Python 3.6.

In versions of Python prior to v3.6, csv.DictReader() returned a regular dict object. From Python 3.6 onwards, it returns the OrderedDict object which you’re seeing. If the video recording was using an older version, say Python 3.5, they’d be getting a dict type back because that was the functionality at the time.

Hope that helps. :slight_smile:

4 Likes

That is a very helpful explanation. Quick follow-up question, Can the exercise be completed with the Ordered dictionary object? How can we return the old dictionary object?

Yes, I would imagine so. The collections.OrderedDict type is a sub-class of dict, so ought to be usable in lieu of a dict (though I haven’t personally verified that).

Quite easily; if you’re using a version of Python which satisfies the condition 3.6 < ver <= 3.8 then csv.DictReader will return a dict automatically as stated in the documentation. :slight_smile:

Failing that, you can probably do a quick-n-dirty conversion by passing the OrderedDict to the dict() builtin.