CodingNic

Data Structures

Sets

Data Structures 26 min read

Sets

Sets

Sometimes you only want unique values.

For example:

  • Unique tags
  • Unique usernames
  • Unique numbers
  • Unique categories

If the same value appears many times, you may want to keep only one copy.

Python gives us sets for this.

What Is a Set?

A set is a collection of unique values.

This means duplicate values are removed automatically.

Sets use curly brackets {}.

python
numbers = {1, 2, 2, 3, 3}
print(numbers)

Output

text
{1, 2, 3}

Why Sets Matter

Sets help you:

  • Remove duplicates
  • Check values quickly
  • Compare groups of data
  • Store unique items

Creating a Set

python
colors = {"Red", "Blue", "Green"}
print(colors)

Empty Set

Use set().

python
items = set()
print(items)

Output

text
set()

Adding One Item

Use add().

python
colors = {"Red", "Blue"}
colors.add("Green")

print(colors)

Output

text
{'Red', 'Blue', 'Green'}

Adding Many Items

Use update().

python
colors = {"Red"}
colors.update(["Blue", "Green"])

print(colors)

Output

text
{'Red', 'Blue', 'Green'}

Removing with remove()

Removes an item.

python
colors = {"Red", "Blue", "Green"}
colors.remove("Blue")

print(colors)

Output

text
{'Red', 'Green'}

Safe Remove with discard()

Does nothing if item is missing.

python
colors = {"Red", "Green"}
colors.discard("Blue")

print(colors)

Output

text
{'Red', 'Green'}

Remove Random Item with pop()

Sets are unordered, so pop() removes any item.

python
colors = {"Red", "Blue", "Green"}
colors.pop()

print(colors)

Output

text
Output may be different each time

Clear All Items

python
colors.clear()
print(colors)

Output

text
set()

Copy a Set

Use copy().

python
a = {1, 2, 3}
b = a.copy()

print(b)

Output

text
{1, 2, 3}

Check If Item Exists

Use in.

python
colors = {"Red", "Blue"}

print("Red" in colors)
print("Green" in colors)

Output

text
True
False

Length of a Set

Use len().

python
colors = {"Red", "Blue", "Green"}
print(len(colors))

Output

text
3

Loop Through a Set

python
colors = {"Red", "Blue", "Green"}

for color in colors:
    print(color)

Note

Set order is not guaranteed.

The printed order may change.

Set Operations

Use:

python
a = {1, 2, 3}
b = {3, 4, 5}

Union

All unique items from both sets.

python
print(a | b)

Output

text
{1, 2, 3, 4, 5}

Intersection

Only shared items.

python
print(a & b)

Output

text
{3}

Difference

Items in first set only.

python
print(a - b)

Output

text
{1, 2}

Symmetric Difference

Items in one set or the other, but not both.

python
print(a ^ b)

Output

text
{1, 2, 4, 5}

Subset

Checks if all items in one set exist in another.

python
small = {1, 2}
big = {1, 2, 3}

print(small.issubset(big))

Output

text
True

Superset

Checks if a set contains all items of another set.

python
print(big.issuperset(small))

Output

text
True

Disjoint

Checks if two sets share no items.

python
x = {1, 2}
y = {4, 5}

print(x.isdisjoint(y))

Output

text
True

Code Along

python
tags = {"python", "coding", "python"}
tags.add("course")

for tag in tags:
    print(tag)

Mini Challenge

Build a unique skills manager.

Steps:

  • Create a set with:
    Python, HTML, Python
  • Print the set
  • Add CSS
  • Remove HTML
  • Check if Python exists
  • Print the union of your set with:
    {"Git", "CSS"}
  • Print the final set

Expected output:

text
{'Python', 'HTML'}
True
{'Python', 'CSS', 'Git'}
{'Python', 'CSS'}

Real World Use Case

Programs use sets for tags, unique users, duplicate removal, permissions, and comparing groups of data.

Quiz

  1. What is a set?
  2. What happens to duplicate values?
  3. What does add() do?
  4. What does intersection return?
  5. What does symmetric difference return?

Assignment

Create two sets of favorite foods. Print the union, intersection, and difference.

Summary

You learned that sets store unique values and how to add, remove, copy, compare, and perform powerful set operations in Python.