Learn Python: Sets and Set Operations

Sets and Set Operations

A set in Python is an unordered collection of unique elements. Sets are handy for membership checks and eliminating duplicates because each item can appear only once. They also offer efficient union, intersection, and difference operations, which mirror mathematical set operations. This lesson explains how to create and manipulate sets in Python within your Visual Studio Code environment.

What will be covered

• Defining sets with curly braces {} or using set().
• Adding and removing elements in a set.
• Checking membership with in.
• Performing union, intersection, difference, and symmetric difference.
• Converting between sets and lists (e.g., for deduplicating data).

Creating Sets

You can make a set using curly braces or the set() constructor. Elements in a set must be unique, and if you add duplicates, Python ignores the extras. Sets ignore order, so you can’t rely on items being in any particular sequence.

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

empty_set = set()
print(empty_set)

Notice “apple” is listed twice in the curly braces, but the resulting set only holds one “apple” because duplicates are removed automatically.

Adding, Removing, and Checking Membership

Insert a new element with .add(). Remove an element with .remove() (which raises an error if the item isn’t there) or .discard() (which ignores missing items). You can also use in to check if something is in the set.

fruits.add("orange")
print(fruits)

fruits.remove("banana")
print(fruits)

if "cherry" in fruits:
    print("Cherry is in the set.")

If you use .remove() on something that’s not in the set, Python raises a KeyError. .discard() avoids that error by doing nothing if the item doesn’t exist.

Mathematical Set Operations

Python sets provide operations similar to those in math:

  • Union (A.union(B) or A | B): all elements in A or B
  • Intersection (A.intersection(B) or A & B): elements in both A and B
  • Difference (A.difference(B) or A - B): elements in A but not in B
  • Symmetric difference (A.symmetric_difference(B) or A ^ B): elements in A or B but not both
evens = {2, 4, 6, 8, 10}
mults_of_3 = {3, 6, 9}

print(evens.union(mults_of_3))
print(evens & mults_of_3)
print(evens - mults_of_3)
print(evens ^ mults_of_3)

These operations allow quick comparisons, like finding common items or unique elements in different sets.

Converting Lists to Sets and Back

When you need to remove duplicates from a list, convert it to a set, then optionally convert back if you need list-like behavior again. The order in which items appear in the resulting list can change because sets are unordered.

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

numbers_no_dups = list(unique_numbers)
print(numbers_no_dups)

This eliminates duplicates but might reorder elements. For many use cases, that reordering doesn’t matter.

Practice Exercises

  1. Create a set of even numbers from 1 to 10 and a set of multiples of 3 from 1 to 10. Print the intersection to see which numbers are in both sets.
  2. Take a list with duplicate entries (e.g., [1, 2, 2, 3, 4, 4, 4, 5]), convert it to a set to remove duplicates, then back to a list. Print the final list.
  3. Given two sets of strings, compute and print the union, difference, and symmetric difference. For example: setA = {"apple", "banana", "cherry"}, setB = {"banana", "cherry", "date", "fig"}.
  4. Add a new element to one of the sets in the previous step using .add() and remove an element with .remove(). Print the set after each operation to confirm changes.
  5. Optional: try .discard() on a set for an item that isn’t present. Print the set to confirm no error occurs and no change is made.

Summary

Sets store unique elements in an unordered fashion, making them ideal for quick membership checks and de-duplication. With set operations like union, intersection, and difference, you can efficiently combine or compare sets of data. When you have a list with duplicate items, converting to a set and back to a list is a quick fix. By understanding sets, you broaden your data-structure toolkit in Python, enabling you to handle tasks that involve uniqueness or membership logic with ease.

Previous
Previous

Learn Python: Dictionaries (Key-Value Pairs)

Next
Next

Learn Python: Defining and Calling Functions