Skip to content
ComputerCS
Start learning

Python Programming · Lesson Supplemental

Sets

Collections with no duplicates and no fixed order — built for membership tests, duplicate removal and set algebra.

01

Learning objectives

  • Create a set and explain why duplicate values disappear automatically.
  • Use the in operator to test membership of a set.
  • Compute union, intersection and difference with |, & and -.
  • Convert a list to a set to remove duplicates, and back to an ordered list with sorted().
  • Explain why sets are unordered and cannot be indexed.
02

Lesson notes

A set is a bag of values with two strict rules: every value appears at most once, and the values have no position. Write a = {1, 2, 3, 4}; feed it duplicates and they vanish, because {1, 2, 2, 3, 3, 3} is exactly {1, 2, 3}. Because there is no position, indexing like a[0] is impossible and Python raises TypeError. When you need the values back in a definite order, wrap them: sorted(a) hands you a sorted list.

What sets lose in order they repay in speed: a membership test, 4 in a, is answered almost instantly even when the set holds a million values, because Python hashes each value and jumps to its slot. The same test on a long list may crawl through every element. So the design rule is: use a list when position matters, use a set when the only question is whether a value is present — attendance registers, enrolled roll numbers, visited web pages.

Sets model the maths you already know. Union a | b is every value in either set; intersection a & b is only the shared values; difference a - b is what is in a but missing from b. A concrete example: physics students union maths students gives the whole science club; the intersection gives students taking both; the difference gives physics-only students. Two or three exam questions every year are exactly this calculation in disguise.

Converting is one call: set(rolls) turns a list into a set and destroys duplicates — the fastest duplicate-removal trick in Python. Going back, sorted(unique) returns an ordered list. One syntax trap: an empty set is set(), not {} — empty braces create an empty dictionary, a distinction the examiner checks deliberately. Grow a set with a.add(5) and shrink it with a.remove(5).

Board tip: when a question says remove duplicates and show in ascending order, the expected answer is two lines plus a sentence: unique = set(data), then print(sorted(unique)), then one line noting that a set is unordered, so sorted() is needed for display. Stating both the property (unordered) and the remedy (sorted) is what separates a full-mark answer from a half-mark one — rehearse the three pieces as a fixed ritual.

03

Worked code examples

Duplicate removal and set algebra

Duplicates collapse immediately, so the literal holds 3 values. The operators |, & and - compute union, intersection and difference; sorted() gives the results a display order.

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(len({1, 2, 2, 3, 3, 3}))
print(sorted(a | b))
print(sorted(a & b))
print(sorted(a - b))
print(4 in a, 9 in a)
Output
3
[1, 2, 3, 4, 5]
[3, 4]
[1, 2]
True False

Cleaning a list of roll numbers

set() keeps one copy of each value, shrinking 6 entries to 4; sorted() then returns the ordered list for printing.

rolls = [21, 7, 21, 15, 7, 9]
unique = set(rolls)
print(sorted(unique))
print(len(rolls), len(unique))
Output
[7, 9, 15, 21]
6 4
04

Key terminology

set

An unordered collection with no duplicate values, written with braces like {1, 2, 3}.

union

All values that appear in either set — the | operator.

intersection

Only the values the two sets share — the & operator.

difference

Values in the first set but not in the second — the - operator.

membership test

Asking whether a value is in a collection, written 'x in a'.

05

Common misconceptions

Common mistake

{} creates an empty set.

Correct understanding

{} creates an empty dictionary. The empty set is written set().

Common mistake

Sets keep elements in insertion order, so a[0] gives the first one added.

Correct understanding

Sets are unordered — there is no first element and indexing raises TypeError. Use sorted(a) when you need an order.

Common mistake

a - b keeps values that are in either set.

Correct understanding

Difference keeps what is in a but NOT in b; values in either set is the union a | b.

06

Check your understanding

1. How many values does len({1, 2, 2, 3, 3, 3}) report?

2. physics = {'Ali', 'Sara'} and maths = {'Sara', 'Hina'}. Which expression lists the students taking BOTH subjects?

3. What is the correct way to create an empty set?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.