Skip to content

Commit 7a3a567

Browse files
committed
Add note about dictionary ordering (or lack thereof
1 parent 62c9990 commit 7a3a567

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

python/lesson3/tutorial.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@ manageable parts. Thankfully, Python has you covered with `.keys()`,
285285
>>> print(my_phone_book.items())
286286
dict_items([('Arya', '+4407485376242'), ('Brienne', '+3206785246863'), ('Cersei', '+14357535455'), ('Davos', '+244562726258')])
287287

288+
> **Important**: dictionaries are *unordered*, which means that while it may
289+
> seem reasonable that you've defined `my_phone_book` above with the keys
290+
> ordered alphabetically, *that's not now how Python stores them*, so the
291+
> results above may differ in order from your output. Typically if you need
292+
> your dictionary to be ordered, you'll use a list of lists, or an
293+
> `OrderedDict` (a topic for another day).
294+
288295
As you can see, `.keys()` and `.values()` do what you'd expect: they return the
289296
keys and values respectively. You may have noticed however that rather than a
290297
`list` or a `tuple`, these methods return `dict_keys` and `dict_values` types.

0 commit comments

Comments
 (0)