Working with Python Sets: Core Properties and Practical Operations
Python sets are unordered, mutable data structures that enforce unique, hashable elements—ideal for duplicate removal and formal set arithmetic.
Core Set Properties and Initialization
Sets cennot contain mutable types like lists, dictionaries, or other sets; valid elements include integers, floats, strings, booleans, and tuples. Empty curly braces {} create dictionaries, so empty sets require the set() constructor.
# Empty set creation only with set()
bag_of_no_dupes = set()
# Populated set with mixed valid types
random_unique_items = {22, "kiwi", False, 7.89, (True, 42)}
print(random_unique_items) # Output order varies: {False, 22, (True, 42), 7.89, 'kiwi'}
# Convert a list to a set to eliminate duplicates
repeated_scores = [85, 92, 85, 79, 92, 88]
unique_scores = set(repeated_scores)
print(unique_scores) # Output: {79, 85, 88, 92}
Common Set Manipulations
Adding Single or Multiple Elements
Use add() for a single element; update() accepts iterables like lists, tuples, or other sets to merge multiple unique entries.
color_set = {"red", "blue"}
color_set.add("green")
print(color_set) # {'blue', 'green', 'red'}
color_set.update(["yellow", "blue", "purple"], ("orange",))
print(color_set) # {'blue', 'green', 'red', 'yellow', 'purple', 'orange'}
Removing Elements
remove() raises a KeyError if the target element is missing, while discard() silent ignores non-existent entries. pop() removes and returns an arbitrary element (raises KeyError on empty sets).
tech_stack = {"Python", "JavaScript", "Docker", "React"}
tech_stack.remove("Docker")
print(tech_stack) # {'React', 'Python', 'JavaScript'}
tech_stack.discard("Kubernetes") # No error
print(tech_stack.pop()) # Returns random element, e.g., 'React'
Membership Testing
Use in or not in for O(1) average-time membership checks, much faster than list/tuple lookups.
active_users = {"alice_dev", "bob_tester", "charlie_admin"}
print("charlie_admin" in active_users) # True
print("david_designer" not in active_users) # True
Set Arithmetic Operations
Union (|, union())
Combines elements from two or more sets without duplicates.
math_course_students = {"Eve", "Frank", "Grace"}
cs_course_students = {"Grace", "Henry", "Eve"}
all_enrolled = math_course_students.union(cs_course_students)
print(all_enrolled) # {'Eve', 'Frank', 'Grace', 'Henry'}
all_enrolled_alt = math_course_students | cs_course_students
print(all_enrolled_alt == all_enrolled) # True
Intersection (&, intersection())
Returns elements present in all input sets.
common_students = math_course_students.intersection(cs_course_students)
print(common_students) # {'Eve', 'Grace'}
common_students_alt = math_course_students & cs_course_students
print(common_students_alt) # {'Eve', 'Grace'}
Difference (-, difference())
Returns elements from the first set only that do not appear in any other set.
math_only = math_course_students.difference(cs_course_students)
print(math_only) # {'Frank'}
cs_only = cs_course_students - math_course_students
print(cs_only) # {'Henry'}
Symmetric Difference (^, symmetric_difference())
Returns elements present in exactly one of the input sets (removes shared entries).
single_course = math_course_students.symmetric_difference(cs_course_students)
print(single_course) # {'Frank', 'Henry'}
single_course_alt = math_course_students ^ cs_course_students
print(single_course_alt) # {'Frank', 'Henry'}
Clearing or Deleting a Set
clear() empties the set without deleting the variable; del removes the variable entirely.
book_collection = {"1984", "To Kill a Mockingbird", "The Great Gatsby"}
book_collection.clear()
print(book_collection) # set()
del book_collection
# print(book_collection) # Raises NameError