Sets

Home / Python / Sets

Sets


1. How to Create a Set
set in Python is an unordered collection of unique elements. Sets do not allow duplicate values and are defined using curly braces {} or the set() function.

Creating a set using curly braces:
my_set = {1, 2, 3, 4, 5}
print(my_set) 
Output: {1, 2, 3, 4, 5}

Empty set: To create an empty set, you must use the set() function, because {} creates an empty dictionary by default.
empty_set = set()
 
Set with duplicate elements: If you add duplicate elements to a set, they are automatically removed because sets only store unique values.
my_set = {1, 2, 3, 1, 2}
print(my_set) 
Output: {1, 2, 3}

Creating a set from other iterables: You can create a set from lists, tuples, or other iterables using the set() function.
list_to_set = set([1, 2, 3, 2, 1])
print(list_to_set) 
Output: {1, 2, 3}


2. Python frozenset
frozenset is an immutable version of a set, meaning that once created, you cannot change its elements (no adding, removing, or updating).
Frozensets are useful when you need a set that should not be modified, such as for keys in a dictionary or elements of a set.

Creating a frozenset:
my_frozenset = frozenset([1, 2, 3, 4, 5])
print(my_frozenset) 
Output: frozenset({1, 2, 3, 4, 5})

Since frozensets are immutable, methods like .add() or .remove() will raise errors.

3. Iteration Over Sets
You can iterate over a set just like any other Python collection using a for loop. The order of elements in a set is not guaranteed, as sets are unordered.

my_set = {10, 20, 30, 40}
for element in my_set:
print(element)
Output:
10
20
30
40  (order may vary)

Membership testing: You can check if an element exists in a set using the in operator.
print(20 in my_set) 
Output: True

print(50 in my_set) 
Output: False

4. Python Set Methods
Python sets provide various methods for performing operations like adding, removing, and modifying set elements, as well as performing set operations like unions, intersections, and differences.

Adding elements: You can add individual elements to a set using .add() and multiple elements using .update().
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) 
Output: {1, 2, 3, 4}


my_set.update([
my_set.update([5, 6])
print(my_set) 
Output: {1, 2, 3, 4, 5, 6}


Removing elements: You can remove elements using .remove(), .discard(), or .pop().

my_set.remove(3)  
Removes 3; raises KeyError if 3 not in the set

my_set.discard(5
Removes 5; does nothing if 5 not in the set

my_set.pop()      
Removes and returns an arbitrary element from the set

Clearing a set: The .clear() method removes all elements from the set.
my_set.clear()
print(my_set) 
Output: set()


Set operations:
Union (|): Combines all elements from both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2) 
Output: {1, 2, 3, 4, 5}


Intersection (&): Returns only the elements that are common to both sets.
print(set1 & set2) 
Output: {3}


Difference (-): Returns the elements that are in the first set but not in the second.
print(set1 - set2) 
Output: {1, 2}


Symmetric Difference (^): Returns elements that are in either set, but not in both.
print(set1 ^ set2) 
Output: {1, 2, 4, 5}