Kahibaro
Discord Login Register

Sets (basics)

What is a Set?

In Python, a set is an unordered collection of unique elements.

Key properties:

Typical uses:

Creating Sets

Using curly braces

You can create a set using {} with comma-separated values:

fruits = {"apple", "banana", "cherry"}
print(fruits)

Remember: the order of printing may be different from how you wrote it.

Using the `set()` function

You can also create a set using the set() function:

numbers = set([1, 2, 3])
print(numbers)

This is especially useful for converting other collections:

  nums_list = [1, 2, 2, 3, 3, 3]
  nums_set = set(nums_list)
  print(nums_set)  # duplicates removed
  letters = set("hello")
  print(letters)  # each unique character once

Empty set

Be careful: {} creates an empty dictionary, not a set.

To create an empty set, use:

empty_set = set()
print(empty_set)

Uniqueness and Duplicates

Sets automatically remove duplicates:

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

If you add the same element multiple times, it only appears once:

colors = set()
colors.add("red")
colors.add("red")
colors.add("blue")
print(colors)  # {"red", "blue"}

You can use this property to quickly remove duplicates from a list:

names = ["Alice", "Bob", "Alice", "Charlie", "Bob"]
unique_names = set(names)
print(unique_names)

If you need a list again, convert it back:

unique_names_list = list(unique_names)
print(unique_names_list)

Basic Set Operations

Membership: checking if something is in a set

Use the in and not in operators:

animals = {"cat", "dog", "bird"}
print("cat" in animals)      # True
print("fish" in animals)     # False
print("fish" not in animals) # True

Membership checks in sets are typically faster than in lists, especially when the collection is large.

Adding elements: `add()`

Use add() to insert a single element:

numbers = {1, 2, 3}
numbers.add(4)
print(numbers)

If the element already exists, nothing changes:

numbers.add(3)  # 3 is already in the set
print(numbers)  # still {1, 2, 3, 4}

Removing elements: `remove()` and `discard()`

numbers = {1, 2, 3}
numbers.remove(2)
print(numbers)     # {1, 3}
numbers.discard(5) # 5 is not in the set, but no error
print(numbers)     # still {1, 3}

If you’re unsure whether an element is present, discard() is safer.

Removing and returning an element: `pop()`

pop() removes and returns some element from the set. Since sets are unordered, you don’t control which one:

items = {"apple", "banana", "cherry"}
item = items.pop()
print("Popped:", item)
print("Remaining:", items)

Clearing all elements: `clear()`

To empty a set:

data = {1, 2, 3}
data.clear()
print(data)  # set()

Set Operations (Like Math Sets)

Sets support operations similar to mathematical sets.

Given:

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

Union: `|` or `union()`

Union combines elements that are in either set.

$$
A \cup B = \{x : x \in A \text{ or } x \in B\}
$$

In Python:

print(a | b)              # {1, 2, 3, 4, 5, 6}
print(a.union(b))         # same result

Intersection: `&` or `intersection()`

Intersection keeps elements that are in both sets.

$$
A \cap B = \{x : x \in A \text{ and } x \in B\}
$$

In Python:

print(a & b)                  # {3, 4}
print(a.intersection(b))      # same result

Difference: `-` or `difference()`

Difference keeps elements that are in the first set but not in the second.

$$
A \setminus B = \{x : x \in A \text{ and } x \notin B\}
$$

In Python:

print(a - b)                   # {1, 2}
print(b - a)                   # {5, 6}
print(a.difference(b))         # {1, 2}

Symmetric difference: `^` or `symmetric_difference()`

Symmetric difference keeps elements that are in either set, but not in both.

$$
A \triangle B = (A \cup B) \setminus (A \cap B)
$$

In Python:

print(a ^ b)                           # {1, 2, 5, 6}
print(a.symmetric_difference(b))       # same result

Subsets and Supersets (Basics)

For simple checks of “is everything in this set also in that set?” you can use:

Example:

small = {1, 2}
big = {1, 2, 3, 4}
print(small <= big)  # True: small is a subset of big
print(big >= small)  # True: big is a superset of small

When to Use Sets (Practical Ideas)

Some beginner-friendly scenarios:

  emails = ["a@example.com", "b@example.com", "a@example.com"]
  unique_emails = set(emails)
  stop_words = {"the", "and", "is", "of"}
  word = "and"
  if word in stop_words:
      print("Skip this word")
  class_a = {"Alice", "Bob", "Carol"}
  class_b = {"Bob", "Dave"}
  both = class_a & class_b           # in both classes
  only_a = class_a - class_b         # only in class A
  everyone = class_a | class_b       # all unique students

Limitations and Things to Remember

Understanding these basics will help you decide when a set is more appropriate than a list, tuple, or dictionary in your programs.

Views: 15

Comments

Please login to add a comment.

Don't have an account? Register now!