Skip to main content

Python Sets

What are Sets?

Sets in Python are unordered collections of unique elements. They are defined by enclosing elements in curly braces {} or by using the set() constructor. Sets are mutable, but they can only contain immutable (hashable) elements.

my_set = {1, 2, 3, 4, 5}

Why Use Sets?

  1. Uniqueness: Sets automatically eliminate duplicate elements.
  2. Fast Membership Testing: Checking if an item is in a set is very efficient.
  3. Mathematical Set Operations: Python sets support operations like union, intersection, and difference.
  4. Removing Duplicates: Sets provide an easy way to remove duplicates from a sequence.
  5. Unordered Nature: When element order doesn't matter, sets can be more efficient than lists.

Creating Sets

# Empty set (note: {} creates an empty dictionary, not a set)
empty_set = set()

# Set with elements
numbers = {1, 2, 3, 4, 5}

# Set from a list (duplicates are removed)
from_list = set([1, 2, 2, 3, 4, 4, 5])

# Set from a string (unique characters)
char_set = set("hello")

# Set comprehension
squares = {x**2 for x in range(10)}

Set Methods

  1. add(elem): Add an element to the set.

    my_set.add(6)
  2. remove(elem): Remove an element from the set. Raises KeyError if not found.

    my_set.remove(3)
  3. discard(elem): Remove an element from the set if it exists.

    my_set.discard(3)  # No error if 3 is not in the set
  4. pop(): Remove and return an arbitrary element. Raises KeyError if empty.

    elem = my_set.pop()
  5. clear(): Remove all elements from the set.

    my_set.clear()
  6. copy(): Return a shallow copy of the set.

    new_set = my_set.copy()
  7. update(iterable): Update the set, adding elements from all iterables.

    my_set.update([6, 7, 8])
  8. intersection(other_set): Return a new set with elements common to the set and all others.

    common = set1.intersection(set2)
  9. union(other_set): Return a new set with elements from the set and all others.

    combined = set1.union(set2)
  10. difference(other_set): Return a new set with elements in the set that are not in the others.

    diff = set1.difference(set2)
  11. symmetric_difference(other_set): Return a new set with elements in either the set or other but not both.

    sym_diff = set1.symmetric_difference(set2)
  12. issubset(other_set): Test whether every element in the set is in other.

    is_subset = set1.issubset(set2)
  13. issuperset(other_set): Test whether every element in other is in the set.

    is_superset = set1.issuperset(set2)

Set Operations

  1. Membership testing

    print(2 in {1, 2, 3})  # True
  2. Iteration

    for item in {1, 2, 3}:
    print(item)
  3. Set comprehension

    even_squares = {x**2 for x in range(10) if x % 2 == 0}
  4. Length

    my_set = {1, 2, 3}
    print(len(my_set)) # 3

Common Use Cases for Sets

  1. Removing Duplicates from a Sequence

    list_with_dupes = [1, 2, 2, 3, 4, 4, 5]
    unique_list = list(set(list_with_dupes))
  2. Membership Testing

    valid_users = {"alice", "bob", "charlie"}
    user = "david"
    if user in valid_users:
    print("Access granted")
    else:
    print("Access denied")
  3. Finding Unique Elements

    text = "hello world"
    unique_chars = set(text)
  4. Mathematical Set Operations

    scientists = {"Einstein", "Newton", "Galileo"}
    physicists = {"Einstein", "Feynman", "Newton"}

    all_scientists = scientists.union(physicists)
    physicist_scientists = scientists.intersection(physicists)
    only_scientists = scientists.difference(physicists)
  5. Removing Specific Elements from a Collection

    numbers = [1, 2, 3, 4, 5, 2, 3, 4]
    remove_set = {2, 4}
    filtered = [num for num in numbers if num not in remove_set]

Best Practices and Tips

  1. Use sets when you need to ensure uniqueness of elements.
  2. Prefer sets over lists for frequent membership testing.
  3. Use frozenset for creating immutable sets (which can be elements of other sets or dictionary keys).
  4. Remember that sets can only contain hashable (immutable) elements.
  5. Use set operations for efficient data processing when dealing with unique elements.

Common Pitfalls

  1. Attempting to Create an Empty Set with

    empty_set = {}  # This creates an empty dict, not a set
    # Correct way
    empty_set = set()
  2. Adding Mutable Objects to Sets

    my_set = {[1, 2, 3]}  # This raises a TypeError
    # Use tuples instead
    my_set = {(1, 2, 3)}
  3. Expecting Ordered Behavior

    my_set = {3, 1, 2}
    print(my_set) # The order is not guaranteed
  4. Modifying a Set While Iterating

    my_set = {1, 2, 3, 4, 5}
    for item in my_set:
    if item % 2 == 0:
    my_set.remove(item) # This can lead to RuntimeError
  5. Using Sets for Data that Needs to Maintain Order

    # If order matters, use a list or an OrderedDict instead
    ordered_data = [1, 2, 3, 4, 5]

Understanding sets and their appropriate use cases can significantly enhance your Python programming, especially when dealing with unique collections of data or performing set-based mathematical operations.