Bigger programs · lesson 3 of 12
Parts in this lesson
A{a, b, c}a set: no repeats
B.add() set()add, or make one
C| & -either, both, only the first
Dinfrom 2.1, fastest on a set

Sets, and choosing a collection

Last updated

A set is a collection with no repeats and no order, built to answer one question fast: is this in it? In this lesson you'll make sets, combine two of them, read the error you get when you ask a set for a position, and then put lists, tuples, dictionaries and sets side by side to choose the right one for a job.

By the end of this lesson you can

  • Make a set, add to it, and use one to remove repeats
  • Combine two sets with |, & and -
  • Explain why a set has no positions, and read the TypeError you get when you ask for one
  • Choose between a list, tuple, dictionary and set for a job

No repeats, no order

A set is written in curly brackets like a dict, but with single values instead of key: value pairs. A set keeps each value once: the second "sci-fi" on line 1 is simply dropped, so genres has 3 values.

.add() puts a value in, and adding one that's already there changes nothing (line 5). set() makes a set from a list, which is the quickest way to remove repeats (line 8).

Step 1 code main.py
Edit the code, then run it again

A set has no order. Python may list its values in any order, and it can differ from one run to the next, so the examples here use sorted() whenever they print a set's values. And watch out: {} on its own is an empty dict. An empty set is set().

Either, both, or only one

Sets can be combined. With Sam's and Ana's books as two sets:

WriteGivesHere
sam | anavalues in either setevery book either of them has read
sam & anavalues in both setsbooks they've both read
sam - anavalues in the first but not the secondbooks only Sam has read
Step 2 code main.py
Edit the code, then run it again

Asking in of a set is fast however big the set is, because a set doesn't search from the front the way a list does. With a few items that makes no difference; with a million it does.

A set has no positions

A set has no order, so it has no first item either. Press Run and read the error.

Step 3 code main.py
Edit the code, then run it again

TypeError: 'set' object is not subscriptable. "Subscript" is Python's word for the square brackets after a value. A set can't be read by position, so square brackets don't work on it at all.

If you need positions, you need a list: sorted(genres)[0] makes a sorted list from the set and takes its first item. Try it.

List, tuple, dictionary or set?

You now know four collections. Each suits a different job:

CollectionWrittenOrderRepeatsCan changeFind things by
list[a, b]yesyesyesposition
tuple(a, b)yesyesnoposition
dict{k: v}yes, as addedkeys no, values yesyeskey
set{a, b}nonoyesin only

Some questions to ask:

  • Do I look things up by a name or number? Use a dict: copies by title, members by member number.
  • Do I only need to know whether something is there, or to remove repeats? Use a set: members who have a book out.
  • Is it a fixed group of different values that belong together? Use a tuple: one book's title, author and year.
  • Otherwise, for things in order that can grow and repeat, use a list: the books on a shelf.
Step 4 code main.py
Edit the code, then run it again

Line 6 shows a list method you haven't seen: .count() gives how many times a value appears. A list allows repeats, so it can count them; a set couldn't.

Check what you learned

Four questions. Pick or type an answer, then check it.

What does this program print?
tags = {"new", "new", "signed", "new"}
print(len(tags))
sam = {"Dune", "Emma"} and ana = {"Emma", "Beloved"}. What is sam & ana?
Why does genres[0] fail when genres is a set?
The library wants the number of copies of each title, found by the title. Which collection fits?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python