Python set is an unordered collection of unique, mutable elements. Set methods help you:
? Handy for membership tests, uniqueness checks, and fast set operations.
add(), remove(), etc.union(), intersection(), and difference() perform mathematical set operations.issubset(), issuperset(), and isdisjoint() compare sets.x in my_set are efficient.A basic set is created using curly braces {} or the set() constructor:
numbers = {1, 2, 3, 4}
fruits = {"apple", "banana", "cherry"}
# Using set() constructor
mixed = set([1, 2, 2, 3, "hello"])
print(mixed) # duplicates removed, order may vary
Python provides several built-in set methods. Common ones:
add(x) – Add a single element x to the set.update(iterable) – Add multiple elements from an iterable (list, tuple, set, etc.).remove(x) – Remove element x; raises KeyError if not present.discard(x) – Remove element x if present; does nothing if absent.pop() – Remove and return a random element.clear() – Remove all elements from the set.union(other) – Return a new set with elements from both sets.intersection(other) – Return a new set with common elements.difference(other) – Return elements present in the first set but not the second.isdisjoint(other) – True if the sets share no elements.issubset(other) – True if all elements of this set are in other.issuperset(other) – True if this set contains all elements of other.Use add() for a single item and update() for multiple items from any iterable.
fruits = {"apple", "banana"}
fruits.add("cherry")
print(fruits) # {'apple', 'banana', 'cherry'} (order may vary)
fruits.update(["orange", "kiwi"])
print(fruits) # {'apple', 'banana', 'cherry', 'orange', 'kiwi'} (order may vary)
remove() raises an error if the item is missing, while discard() silently ignores it. pop() removes and returns a random element, and clear() empties the set.
fruits = {"apple", "banana", "cherry", "orange", "kiwi"}
# remove() - raises KeyError if element not present
fruits.remove("banana")
print(fruits) # 'banana' removed
# discard() - safe remove, no error if element is missing
fruits.discard("mango") # No error even though 'mango' is not in the set
print(fruits)
# pop() - removes and returns a random element
item = fruits.pop()
print("Removed:", item)
print("After pop:", fruits)
# clear() - remove all elements
fruits.clear()
print(fruits) # set()
These methods correspond to mathematical set operations.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# union() - elements from both sets
print(set1.union(set2)) # {1, 2, 3, 4, 5}
# intersection() - common elements
print(set1.intersection(set2)) # {3}
# difference() - elements in set1 but not in set2
print(set1.difference(set2)) # {1, 2}
These methods compare two sets and return True or False based on their relationship.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# isdisjoint() - True if no common elements
print(set1.isdisjoint({6, 7})) # True
print(set1.isdisjoint(set2)) # False (they share 3)
# issubset() - True if all elements are contained in another set
print({1, 2}.issubset(set1)) # True
print({1, 4}.issubset(set1)) # False
# issuperset() - True if set1 contains all elements of another set
print(set1.issuperset({1, 2})) # True
print(set1.issuperset({1, 2, 4})) # False
{'apple', 'banana', 'cherry'}, the element order may change each run, because sets are unordered.remove("banana"), the element "banana" will no longer appear in the set. Trying fruits.remove("mango") would raise a KeyError.discard("mango") does nothing if "mango" is not in the set, and no error is raised.pop() removes a random element; you will see a different value printed sometimes.union() returns a new set that combines elements from both sets without duplicates.intersection() shows only the common elements (e.g. {3} for {1,2,3} and {3,4,5}).difference() is directional: set1.difference(set2) may be different from set2.difference(set1).issubset(), issuperset(), and isdisjoint() always return booleans (True / False) describing how two sets relate.intersection() to quickly find common elements between two collections.difference() to see what is in one set but not in another (e.g. missing permissions, missing IDs).issubset() and issuperset() to check if one group of items is fully contained inside another.isdisjoint() is useful for confirming that two sets share no overlap (e.g. two non-overlapping user groups).append() on sets; they are not supported.students_a and students_b) and try all set methods on them.pop() a few times to see which element is removed and how it changes the set.difference() and intersection() on overlapping sets to understand how elements move between the results.