Sets in python: quick revision, 16 bites

All 16 revision bites from this kit, free and with no sign-up.

What is a Set?

Unordered, unique, and fast.

A **set** is an unordered collection of *unique* elements. Duplicates are automatically removed. Sets are mutable (you can add/remove items) but each item must be *immutable* (e.g., numbers, strings, tuples). They are defined with curly braces `{}` or the `set()` constructor.

Takeaway. A set is an unordered collection of unique, immutable elements.

Creating Sets

Curly braces or set().

Use `{ }` with elements separated by commas, or the `set()` constructor. An empty `{}` creates a *dictionary*, not a set. For an empty set, always use `set()`.

Takeaway. Use `set()` for an empty set; `{}` creates a dictionary.

Set Properties

No order, no duplicates.

Takeaway. Sets are unordered, unique, mutable, and provide fast membership checks.

Mutable Elements Error

Lists can't be in sets.

Takeaway. Only immutable objects (numbers, strings, tuples) can be set elements.

Adding and Removing Elements

add(), remove(), discard().

Takeaway. Use `add()` to insert, `remove()`/`discard()` to delete, and `pop()` to remove an arbitrary item.

Set Operations: Union, Intersection, Difference

Combine, overlap, or subtract.

Sets support mathematical set operations using operators or methods.

Takeaway. Use `|`, `&`, `-`, `^` for union, intersection, difference, and symmetric difference.

Membership Testing

Fast 'in' check.

Use the `in` keyword to check if an element exists in a set. This is extremely fast (O(1) average) compared to lists (O(n)).

Takeaway. Membership testing with `in` is O(1) for sets, making them ideal for lookups.

Removing Duplicates from a List

Quick deduplication trick.

Convert a list to a set to remove duplicates, then convert back to a list if needed. This is a common Python idiom.

Takeaway. Use `list(set(my_list))` to remove duplicates quickly.

Tracking Unique Visitors

Unique user IDs in a set.

Sets are perfect for tracking unique items like user IDs, IP addresses, or tags. Adding a new ID is O(1), and duplicates are automatically ignored.

Takeaway. Sets efficiently store and manage unique items in real-world applications.

Set vs List vs Tuple

Which one to choose?

Takeaway. Use sets for unique, unordered data with fast lookups; lists/tuples for ordered sequences.

Frozen Sets for Immutability

Make a set hashable.

A **frozenset** is an immutable version of a set. It can be used as a dictionary key or as an element of another set. Create it with `frozenset()`.

Takeaway. Use `frozenset` when you need an immutable, hashable set.

Set Comprehension

Create sets in one line.

Use **set comprehension** with `{ }` to generate sets dynamically, similar to list comprehension.

Takeaway. Set comprehensions offer a concise way to build sets from iterables.

Remember Set Operators

Think Venn diagrams.

Visualize **Venn diagrams**: union is everything, intersection is the overlap, difference is what's left in one circle, symmetric difference is the non-overlapping parts.

Takeaway. Venn diagrams help recall set operations intuitively.

Sets Use Hashing

Hash tables power sets.

Python sets are implemented using **hash tables**. This is why membership testing is O(1) and why elements must be hashable (immutable).

Takeaway. Sets rely on hash tables for fast operations.

Common Set Methods Summary

Quick method reference.

Takeaway. Master these methods to manipulate sets effectively.

Checking Subsets and Supersets

issubset() and issuperset().

Check if one set is contained within another using `issubset()` (`<=`) and `issuperset()` (`>=`).

Takeaway. Use `issubset()`/`issuperset()` or `<=`/`>=` to compare set containment.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages