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 trial

Python Introducing Dictionaries Introducing Dictionaries Recap of Introducing Dictionaries

Erik Embervine
seal-mask
.a{fill-rule:evenodd;}techdegree
Erik Embervine
Python Development Techdegree Student 2,442 Points

dictionaries are ordered, or not ordered?

the first quiz question says dictionaries are not ordered. i understand dictionaries are not indexed by numbers, but that was a different quiz question. what does the first quiz question mean when it says dictionaries are not ordered?

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

You are indeed correct! Dictionaries are not ordered.

But, sometimes the implementation of Python you are working on will have sorting by key but it is not guaranteed to be such in all Python versions.

You can have an OrderedDicttype. (e.g. OrderedDict can be used if the code specification for your project requires "legacy" Python, that is, versions prior to 3.7.)

Read about it here:

RealPython "OrderedDict vs dict in Python: The Right Tool for the Job"

https://realpython.com/python-ordereddict

Ruby Wu
Ruby Wu
7,989 Points

As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Jeff Muday
Jeff Muday
Treehouse Moderator 28,716 Points

You make a good point!

You may be surprised to discover on your job, certain codebases/libraries will require legacy versions of Python. Believe it or not some companies are still using Python 2.7 because of library constraints.

It is still a best practice to use OrderedDict where the dictionaries are meant to be ordered. It is possible that this esoterica will pop up in coding interviews!

Good luck on your Python journey!