Python Sets

Sets in Python are a powerful and versatile data type used to store unique, unordered elements. They are highly optimized for operations like membership testing, union, intersection, and difference.



What is a Set?:


Example:

fruits = {"apple", "banana", "cherry"}
print(fruits)  # Output: {'banana', 'apple', 'cherry'} (order may vary)


Creating a Set:

You can create a set in two ways:


Example:

# Method 1: Using curly braces
colors = {"red", "green", "blue"}

# Method 2: Using set()
numbers = set([1, 2, 3, 4])

# Empty set (Note: { } creates a dictionary, not a set)
empty_set = set( )


Set Operations

Sets support various operations to manipulate and query the data they contain:


Example:

# Adding elements
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)  # Output: {'banana', 'apple', 'cherry', 'orange'} (order may vary)

# Updating elements
fruits.update(["kiwi", "grape"])
print(fruits)  # Output: {'banana', 'apple', 'cherry', 'orange', 'kiwi', 'grape'} (order may vary)

# Removing elements
fruits.remove("banana")
print(fruits)  # Output: {'apple', 'cherry', 'orange', 'kiwi', 'grape'} (order may vary)

# Union of sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set_union = set1.union(set2)
print(set_union)  # Output: {1, 2, 3, 4, 5}

# Intersection of sets
set_intersection = set1.intersection(set2)
print(set_intersection)  # Output: {3}

# Difference of sets
set_difference = set1.difference(set2)
print(set_difference)  # Output: {1, 2}


Removing Elements:


Example:

s = {1, 2, 3, 4}
s.remove(3)    # {1, 2, 4}
s.discard(10)  # No error
s.pop()        # Removes random element (e.g., 1)
s.clear()      # set()


Checking Membership:

You can check if an element is in a set using the in keyword.


Example:

fruits = {"apple", "banana"}
print("apple" in fruits)  # True
print("mango" in fruits)  # False


Set Methods:

Method Description
add( ) Adds an element to the set
remove( ) Removes an element; raises error if not found
discard( ) Removes an element if present (no error if not found)
pop( ) Removes and returns a random element
clear( ) Removes all elements from the set
update( ) Adds elements from another set or iterable
union( ) Returns a new set with all elements from both sets
intersection( ) Returns a set with common elements
difference( ) Returns a set with elements not in another set
copy( ) Returns a shallow copy of the set
symmetric_difference( ) Returns elements in either set, but not both


Frozen Sets (Immutable Sets):


Example:

fs = frozenset([1, 2, 3])
# fs.add(4) → Error (frozen sets are immutable)


Set Operations (Union, Intersection, Difference):

Union ( | or union( ) )

Combines elements from both sets.


Example:

A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)          # {1, 2, 3, 4, 5}
print(A.union(B))     # Same output


Intersection ( & or intersection( ) )

Returns common elements.


Example:

print(A & B)          # {3}
print(A.intersection(B))  # Same output


Difference ( - or difference( ) )

Returns elements in A but not in B.


Example:

print(A - B)          # {1, 2}
print(A.difference(B)) # Same output


Symmetric Difference ( ^ or symmetric_difference( ) )

Returns elements in either set but not both.

Example:


print(A ^ B)          # {1, 2, 4, 5}


Practical Use Cases:



Example:

# Remove duplicates from a list
names = ["Alice", "Bob", "Alice", "Charlie"]
unique_names = set(names)  # {'Alice', 'Bob', 'Charlie'}


Tricky Concepts & Common Mistakes:

1. Sets are Unordered


Example:

s = {3, 1, 2}
print(s)  # May print {1, 2, 3} or any order


2. Sets Cannot Contain Mutable Elements


Example:

# invalid_set = {1, [2, 3]} → Error (unhashable type: 'list')
valid_set = {1, (2, 3)}  # Tuples are allowed (immutable)


3. pop( ) Removes a Random Element


Example:

s = {5, 10, 15}
print( s.pop( ) )  # Could be 5, 10, or 15


4. { } is Not an Empty Set!


Example:

empty_set = set( )


5. len( ) - Finding the Length of a Set


Example:

fruits = {"apple", "banana", "cherry", "apple"}  
print(len(fruits))  # Output: 3 (because "apple" is duplicate)


6. type( ) - Checking Data Type of a Set


Example:

fruits = {"apple", "banana", "cherry", "apple"}  
print(len(fruits))  # Output: 3 (because "apple" is duplicate)