Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialColby Work
20,422 PointsWhy 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
Treehouse Moderator 68,457 PointsIt 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
Colby Work
20,422 PointsColby Work
20,422 Pointsah 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!