Lists, Dictionaries and Sets
Level 2 · Values and variables
How a program keeps many values under one name. Lists and their index numbers, key and value pairs, sets with no repeats, and tuples.
Course contentsLesson 8 of 18
Level 1 · What a program is
Level 2 · Values and variables
Level 3 · Decisions and loops
Level 4 · Functions
Level 5 · Errors and debugging
Level 6 · Good code habits
After this lesson you can
- Read an item from a list by its index
- Look a value up in a dictionary by its key
- Pick a list, dictionary, set or tuple for a job
13 mincovers 5 glossary entries
Worth reading first: Data Types in Python
How to keep many values under one name
A collection keeps many values under one name. Python has four common ones: the list, the dictionary, the set and the tuple. A list of 3 names is written ["Ana", "Ben", "Cy"]. Each one is built for a different job, so picking the right one makes the rest of the code simpler.
A list is Python's version of an array: values kept in order, each in a numbered spot. That number is the index, and it starts at 0, not 1. So in names = ["Ana", "Ben", "Cy"], names[0] is Ana and names[2] is Cy. names[-1] counts from the end and also gives Cy.
Starting at 0 trips up almost everyone at first. A list of 3 items has spots 0, 1 and 2. There is no spot 3, so names[3] fails with an IndexError. len(names) gives 3, which is one more than the last index.
A dictionary stores pairs: a key and its value. prices = {"tea": 2.5, "cake": 3.0} pairs each item with a price. You look a value up by its key, as in prices["tea"], rather than by a number. Since Python 3.7, a dictionary keeps its pairs in the order you added them.
A set holds values with no repeats. Put 1, 2, 2 and 3 into a set, and it keeps 1, 2 and 3. It has no order and no index. Sets are fast at one question: is this value in here? That makes them good for things like a list of banned words.
A tuple is like a list that cannot change once it is made. It is written with round brackets, such as (3, 4) for a point on a grid. Use one for a small group of values that belong together and should stay fixed.
Two mix-ups are common. {} on its own makes an empty dictionary, not an empty set. Write set() for that. And a dictionary key must be unique: add "tea" twice and the second value simply replaces the first.
names = ["Ana", "Ben", "Cy"] # list
print(names[0], names[-1]) # Ana Cy
prices = {"tea": 2.5, "cake": 3.0} # dictionary
print(prices["tea"]) # 2.5
tags = {"red", "blue", "red"} # set, no repeats
print(len(tags)) # 2
point = (3, 4) # tuple, fixedWorked example
Lucy builds a class register
Lucy keeps 28 pupils in a list, so she can print them in seating order. Their test marks go in a dictionary, keyed by name.
She wants the clubs the pupils joined, with each club named once. She puts all 41 sign-ups into a set and gets 6 clubs back.
The words for this lesson5
Check yourself
In names = ["Ana", "Ben", "Cy"], what is names[1]?
Finished this lesson?
Marking it complete also marks its 5 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.