Python Set Practice With Five Beginner Exercises
A learner in my Thursday club brought me 34 lines of code that found the duplicate names in a sign-in sheet. Two nested loops, a flag variable, an index comparison to avoid matching a name against itself, and one bug she had not found yet.

A learner in my Thursday club brought me 34 lines of code that found the duplicate names in a sign-in sheet. Two nested loops, a flag variable, an index comparison to avoid matching a name against itself, and one bug she had not found yet. The sheet had 180 rows. Her program worked, mostly, and she was proud of it, and I felt bad telling her the same job is one line with a set.
Sets are the part of Python that beginners meet last and need early. They answer two questions fast: have I seen this before, and what is in both of these groups. Nearly every small project hits those questions, from a word game to an attendance tracker, and solving them with lists produces exactly the kind of code above. The exercises further down take about forty minutes and are built around those two questions rather than around syntax.
What a Set Gives You That a List Does Not
Three differences, and only three worth memorising at this stage.
A set refuses duplicates. Add the same value twice and the second add does nothing, silently. That is the behaviour that turns a 34 line duplicate finder into one line, and it is also the behaviour that will lose data you wanted to keep if you are careless.
A set has no order. There is no first item, no indexing, no slicing. Printing a small set often looks sorted, which fools people into relying on it. Do not.
A set finds things almost instantly. Checking name in my_list walks the list from the start, so the time grows with the length. Checking name in my_set jumps straight to a bucket and the length barely matters. I ran this with a club group on a list of 60,000 words: looking up 5,000 words against the list took about 11 seconds on a school laptop, and against a set it finished before anyone could read the timer. That demonstration converts more learners than any explanation I have tried.
Three Rules That Catch Nearly Everyone
Empty curly brackets do not make a set. They make an empty dictionary, which is a historical accident you simply have to remember. Use set() to start empty, and curly brackets only when you already have values to put inside.
A set can only hold hashable values. Numbers, strings and tuples go in. Lists and dictionaries do not, and the error message says unhashable type, which is not a helpful phrase to a person in week six. If you hit it, the usual fix is converting the inner lists to tuples.
The methods have two spellings with different manners. add puts one item in, update puts in everything from another collection. remove raises an error if the item is absent, discard does not. Choosing discard because it never complains is how beginners hide typos in variable names for a week.
Five Exercises to Work Through This Week
Do them in order and write each one in its own file. The point is not the answers, which are short. The point is predicting the output before you run anything.
- Dedupe a register. Type out a list of 12 names with 3 repeats. Print the length of the list, convert it with
set(), print the length again. Then print the set three times in one program and look carefully at the order. Write down in a comment whether it changed. - Membership against a word list. Build a set of 20 words you consider easy. Ask for a word with
input()and report whether it is in the set. Now do the same with a list instead and convince yourself the code reads almost identically, which is why the speed difference surprises people. - Two weeks of attendance. Make two sets, week one and week two, with some overlap. Use
andbetween them for who came both weeks, the minus sign for who stopped coming, and the vertical bar for everyone who came at all. Print all three with labels. This single exercise covers most real set use. - Unique letters in a sentence. Count the distinct letters in a sentence you type yourself. Your first answer will be wrong because of the spaces and the capital letters, and fixing that is the exercise. Aim to end up with a count you can verify by hand on paper.
- Rewrite and time the duplicate finder. Write the nested loop version first, the slow one. Then write the set version. Time both with
time.perf_counter()around each, on 200 items and then on 20,000. Record the four numbers. Those numbers are the whole lesson of this page.
If exercise three gives you trouble, go back to Python list practice first and make sure building a list from a loop feels automatic. Sets on top of shaky list skills is a frustrating week.
Why Just Use a Set, It Is Faster Is the Wrong Lesson
Every tutorial ends at the timing result, and that is where I think they do harm. Learners walk away with a rule, convert every list in their project to a set, and break things.
Two reasons the rule fails. First, you paid for the speed by throwing away the order, and in most beginner projects the order is the data. A leaderboard, a chat log, a sequence of moves, all of these stop making sense once the order goes. Second, building the set costs one full pass through the list, so if you check membership twice there is no saving at all. The crossover in my rough measurements sits somewhere around a few hundred lookups, and below that the choice is about readability rather than speed.
There is also a neater answer to the dedupe problem that nobody teaches beginners. list(dict.fromkeys(names)) removes duplicates and keeps the original order, because dictionaries have kept insertion order since Python 3.7. It is one line, same as the set version, and it does not quietly shuffle your data. I show both and let the group argue about which reads better.
How I Check These in Club
I ask for the prediction, not the program. Before anyone runs exercise one, they write the two length values on paper. Before exercise three, they write the three name lists by hand. Then they run it. A learner who predicted correctly understood something; a learner whose program printed the right answer may have only guessed at syntax until the errors stopped.
The same approach works for dictionaries, and the pairing is natural because a set is close to a dictionary with the values thrown away. If you want the next step, dictionary practice follows on directly, and the rest of the beginner track sits in Python and practice.
FAQ
When should a beginner use a set instead of a list?
Use a set when duplicates are meaningless to you and you need to ask whether something is already present. Use a list when order matters or you need to reach items by position.
Why does my empty curly brackets variable behave like a dictionary?
Because it is one. Curly brackets with nothing inside create an empty dictionary in Python, so an empty set has to be written as set().
Can I sort a set?
You can call sorted() on it, which gives you back a list in order. The set itself stays unordered, so sort at the moment you print rather than trying to keep a set tidy.
Are sets covered in beginner Python courses?
Often only in passing, usually after dictionaries. That is why so many learners write nested loop duplicate finders well into their second month, and why one evening on the exercises above pays off quickly.
More in Python and Practice
Python and Practice
Python Enumerate and Zip Practice With Five Exercises
A student at our Tuesday club spent eleven minutes last month hunting a bug in four lines of code.
Python and Practice
Python List Comprehension Practice With Five Exercises
A student in my Tuesday group handed me a line of code last spring that was 118 characters long, had two for clauses and an if...
Python and Practice
Python Try Except Exercises With Five Beginner Problems
Your program works perfectly until someone types "twelve" instead of 12, and then it crashes with a wall of red text.