Exercise 43
Doing Things to Dictionaries
You are now going to learn about the dictionary data structure in Python. A dictionary (or “dict”) is a way to store data just like a list, but instead of using only numbers to get the data, you can use almost anything. This lets you treat a dict like it’s a database for storing and organizing data.
Let’s compare what dicts can do to what lists can do. You see, a list lets you do this:
1 >>> things = ['a', 'b', 'c', 'd']
2 >>> print(things[1])
3 b
4 >>> things[1] = 'z'
5 >>> print(things[1])
6 z
7 >>> things
8 ['a', 'z', 'c', 'd']
You can use numbers to “index” into a list, meaning you can use numbers to find out what’s in lists. You should ...
Get Learn Python the Hard Way: A Deceptively Simple Introduction to the Terrifyingly Beautiful World of Computers and Data Science, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.