Welcome to sets, a powerful and often underappreciated data structure in Python! Unlike lists and tuples, which can hold duplicate items and maintain a specific order, sets are all about uniqueness and lack of order. Think of a set as a bag of distinct marbles – each marble is unique, and the order in which you pull them out doesn't matter.
The primary characteristic of a set is that it contains only unique elements. If you try to add an element that's already in the set, it will simply be ignored. This makes sets incredibly useful for tasks like removing duplicates from a list or checking for membership quickly.
Let's see how to create a set. You can use curly braces {} or the set() constructor. Remember, an empty set cannot be created with {} because that syntax is used for dictionaries. For an empty set, you must use set().
fruits = {"apple", "banana", "cherry"}
print(fruits)
numbers = set([1, 2, 3, 2, 1])
print(numbers)
empty_set = set()
print(type(empty_set))As you can see in the output for numbers, the duplicate 2 and 1 were automatically removed, resulting in a set of unique integers. Also, notice that the order might not be the same as the order you defined them in the list, reinforcing the unordered nature of sets.
Sets are mutable, meaning you can add or remove elements from them. Here's how you can modify a set:
my_set = {"red", "green", "blue"}
# Adding an element
my_set.add("yellow")
print(my_set)
# Adding multiple elements
my_set.update(["orange", "purple"])
print(my_set)
# Removing an element (raises KeyError if not found)
my_set.remove("green")
print(my_set)
# Removing an element (does nothing if not found)
my_set.discard("black")
print(my_set)The remove() method is strict: if the element you try to remove isn't present, it will raise a KeyError. The discard() method, on the other hand, is more forgiving and won't raise an error if the element is missing. This makes discard() safer when you're unsure if an element exists.