๐Ÿ“ฆ What is a Set?

A set is an unordered collection of unique items. Python enforces uniqueness automatically โ€” insert the same value twice and it stays once. Sets are fast for membership testing because they use hash tables underneath.

my_set = {item1, item2, item3}

Two key traits: (1) no duplicates, and (2) checking if x in my_set takes O(1) time โ€” constant, not proportional to the set's size. This makes sets perfect for deduplication and fast lookups.

๐Ÿ” Key Traits of Sets

Uniqueness
{1, 2, 1}
Duplicates are silently dropped; the set holds {1, 2}
Unordered
{3, 1, 2}
No index โ€” you can't access items by position
Mutable
add(), remove()
You can add and remove items after creation
Fast Lookup
x in set โ†’ O(1)
Membership tests are constant-time, unlike lists
Hashable Items
str, int, tuple, not list
Set elements must be immutable (hashable)
Set Operations
union, intersection, difference
Boolean operations on sets work like Venn diagrams

๐Ÿข Business Scenario: Email Deduplication at SouthBank India

๐Ÿฆ
Real-world example
SouthBank India Marketing Campaign

SouthBank India is running a marketing campaign. They've collected customer emails from three sources: (1) their core database, (2) a partner bank, and (3) recent sign-ups. Some emails appear in multiple sources. They need to:

  • Deduplicate the email list (remove repeats)
  • Find which customers are in all three sources (high-value)
  • Find customers only in their core database (to nurture)
3
Email sources
2,847
Total rows (with dups)
1,934
Unique emails

๐Ÿ’ก Step 1: Deduplicating Email Lists with set()

Convert a list to a set to automatically remove duplicates. This is the simplest form of deduplication.

Deduplicating emails
Output:Total emails (with dupes): 8 Unique emails: 4 Deduped list: ['alice@southbank.com', 'bob@southbank.com', 'charlie@partner.com', 'diana@southbank.com']

๐ŸŽฏ Step 2: Finding Common Customers (Intersection)

Use & or .intersection() to find emails appearing in multiple sources โ€” these are your most-engaged customers.

Finding customers in all three sources
Output:High-value customers (in all sources): {'bob@southbank.com'} Core + Partner: {'bob@southbank.com', 'charlie@partner.com'} Core only: {'charlie@partner.com'}

๐Ÿ“Š Step 3: Planning the Campaign with Union & Difference

Use | (union) to combine lists, and - (difference) to exclude certain groups โ€” e.g., send to everyone except those who already engaged.

Campaign targeting: who to email
Output:Campaign email list: {'alice@southbank.com', 'bob@southbank.com', 'diana@southbank.com'} Count: 3 All segments (premium + standard): {'alice@southbank.com', 'bob@southbank.com', 'diana@southbank.com'} Overlap: None

โšก Step 4: Why Sets Are Fast โ€” O(1) vs. O(n)

The real power of sets: membership testing is constant-time. Test with timeit to see the speed difference when checking if an email is in a list vs. a set.

Timing set vs list membership
Output (typical):List membership test: 12.450 ms per 1000 checks Set membership test: 0.023 ms per 1000 checks Set is 540x faster!

๐ŸŒ Where Sets Show Up in Real Data Work

Common use cases

๐Ÿฆ
Banking: Fraud Detection
Find transactions with unique card numbers not in the approved merchant list. If card is not in approved_merchants, flag it.
๐Ÿ“ฑ
E-commerce: Inventory Dedup
Remove duplicate product IDs from bulk uploads. At Jumia Kenya, thousands of sellers upload SKUs โ€” sets instantly deduplicate.
๐Ÿฅ
Healthcare: Insurance Claims
Find patients appearing in multiple claim batches. Use patient_set1 & patient_set2 for overlap detection.
๐ŸŽฌ
Streaming: Tag Overlap
Find movies tagged with both "action" and "thriller". action_movies & thriller_movies gives the intersection.

๐Ÿค– How AI & ML Use Sets

Every recommendation engine and text classifier leans on sets. When a model learns "which words appear in spam emails", it builds a set of spam-words โ€” then checks new emails against that set in O(1) time. For large text corpora, this speed difference is enormous. In NLP feature engineering, you often use sets to find common_words_in_domain_1 & common_words_in_domain_2 โ€” words that define both genres of text.

NLP feature-engineering spam-detection recommendation-engines

๐ŸŽฏ Mini Challenge

Tag Overlap Analysis

You have two user segment tags from Jumia Kenya. segment_a = {"electronics", "premium", "urban"} and segment_b = {"premium", "mobile-first", "deals"}. Find: (1) tags in both segments (intersection), (2) tags unique to segment_a (difference), (3) all tags combined (union). Then find what you'd exclude: tags in segment_b that are not in segment_a.

Use & for intersection, - for difference, and | for union. Remember sets are unordered, so print them or convert to sorted lists for consistent output.

๐ŸŽค Interview Questions

A list is ordered and allows duplicates: [1, 2, 2, 3] has four items. A set is unordered and enforces uniqueness: {1, 2, 3} has three items. Lists support indexing (list[0]); sets do not. Use lists when order matters or duplicates are intentional; use sets for fast membership testing and deduplication.
O(1) โ€” constant time. Sets use hash tables internally, so lookup is constant regardless of the set's size. Compare with lists: checking if x in list is O(n), meaning it scales with list length. For large datasets, this difference is dramatic: searching a list of 1 million items takes ~500,000 comparisons on average; a set takes one hash calculation.
remove() raises KeyError if the item doesn't exist. discard() silently does nothing if the item is missing. In production code, use discard() when you're not certain the item is in the set โ€” it won't crash your pipeline. Use remove() when the item must be there, and you want to catch the error immediately.
Set elements must be hashable โ€” immutable objects that hash to a consistent value. tuple is immutable, so it's hashable. list is mutable; you can change its contents after creation, which would break the hash table. If you want to store a group of items in a set, wrap them in a tuple: set_of_combos = {(1, 2), (3, 4)}. This is crucial when storing database keys or multi-field records.
frozenset is immutable โ€” you can't add or remove items after creation. Use it when you need a set as a dictionary key or want to ensure it doesn't change. Example: if you're building a cache of common email sets, use frozenset keys so they can be hashed in a dict. Regular sets are unhashable, so {set1, set2} fails, but {frozenset1, frozenset2} works. Also use frozenset when passing sets between functions and you don't want them modified.

๐Ÿง  Quick Quiz

Question 1 of 5
What does set([1, 2, 2, 3]) return?
Question 2 of 5
What is the time complexity of x in my_set?
Question 3 of 5
Which operator finds the intersection of two sets?
Question 4 of 5
What happens when you call remove() on a missing item?
Question 5 of 5
Why can't you put a list inside a set?
0
/5

๐Ÿ“‹ Quick Cheatsheet

TaskSyntaxNotes
Create a sets = {1, 2, 3} or set(list)Curly braces, not square brackets
Add items.add(4)Silently ignores if item exists
Remove item (safe)s.discard(4)No error if missing
Remove item (strict)s.remove(4)KeyError if missing
Intersections1 & s2 or s1.intersection(s2)Items in both
Unions1 | s2 or s1.union(s2)Items in either
Differences1 - s2 or s1.difference(s2)Items in s1 but not s2
Membership testif x in s:O(1) โ€” constant time
Frozensetfs = frozenset([1, 2])Immutable; can be dict key

โš ๏ธ Common Mistakes

Using empty curly braces for an empty set
{} creates an empty dict, not a set. Use set() for an empty set. Confusing the two can silently break your code.
Trying to index a set
Sets are unordered, so my_set[0] raises TypeError. If you need the first item, convert to a list: list(my_set)[0]. But remember the order is unpredictable.
Using a list as a set element
{[1, 2], [3, 4]} raises TypeError: unhashable type. Use tuples instead: {(1, 2), (3, 4)}. Tuples are immutable and hashable.
Forgetting that sets are unordered
Never assume set([1, 2, 3]) will iterate in that order. If order matters, convert to a sorted list: sorted(my_set). Sets are designed for membership testing, not ordering.

โœฆ Key Takeaways

  • Sets automatically enforce uniqueness โ€” duplicate values are silently dropped
  • Membership testing on sets is O(1) โ€” orders of magnitude faster than lists for large data
  • Use & for intersection, | for union, - for difference โ€” like Venn diagrams
  • Set elements must be hashable (immutable): str, int, tuple yes; list, dict, set no
  • discard() is safer than remove() in production code when item may be missing
  • frozenset is immutable and can be used as a dict key or in another set
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You've learnt sets.
Now master data structures and deduplication.

Join the Best AI Program: The AI-Era Analyst โ€” 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Analyst โ†’