Python Using Databases in Python Our Diary App Switching It Up

Why is OrderedDict written like a list?

so I thought dictionaries are written like {key: value, key: value}, but in this video, the ordered dictionary is written as a list of tuples. Is OrderedDict general for multiple kinds of collections, and not necessarily just dictionaries? Please help me understand.

thanks.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 59,892 Points

It turns out there are many ways to specify the initial data in a dict [see docs):

To illustrate, the following examples all return a dictionary equal to {"one": 1, "two": 2, "three": 3}:

>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> a == b == c == d == e
True

In the video Kenneth is using the style "d" above.

See docs for more on OrderedDict objects

ah thank you, that makes sense. for some reason, I wasn't thinking about OrderedDict in the same way dict() works, or list(), etc... now I see it. thanks again!