How to add an Element to a Set

Posted in Python by Dirk - last update: Feb 07, 2024

In Python, a set is an unordered collection of unique elements. To add elements to a set, you can use the add() method for individual elements or the update() method for adding elements from an iterable. Here’s the general syntax and explanations for different cases:

Add an element to an empty set

We start with the creation of an empty set (my_set). The add() method is then used to insert the element new_element into the set.

After this operation, the set contains only one element, new_element.

# Creating an empty set
my_set = set()

# Adding an element to the set
my_set.add("new_element")

# Printing the result
print(my_set)

Result:

{'new_element'}

Add a New Element to a Set

Here, we have a set (existing_set) with elements 1, 2, and 3. The add() method is employed to include a new element, 4, into the set. After execution, the set contains the elements 1, 2, 3, and 4.

# Creating a set with existing elements
existing_set = {1, 2, 3}

# Adding a new element to the set
existing_set.add(4)

# Printing the result
print(existing_set)

Result:

{1, 2, 3, 4}

Add an Element in a Set that Already Exists

In this case, the set existing_set initially has elements 1, 2, and 3. When the add() method is used to insert the element 2, there is no error raised. However, since sets only contain unique elements, the set remains unchanged, as it still contains only 1, 2, and 3.

# Creating a set with existing elements
existing_set = {1, 2, 3}

# Adding an element to the set that already exists
existing_set.add(2)

# Printing the result
print(existing_set)

Result:

{1, 2, 3}

Adding An Iterable to a Set

Here, existing_set has elements 1, 2, and 3. The update() method is used to add elements from the iterable new_elements (containing 4, 5, and 6) to the set. After the operation, existing_set contains elements 1, 2, 3, 4, 5, and 6.

# Creating a set with existing elements
existing_set = {1, 2, 3}

# Adding elements from an iterable (e.g., list or tuple)
new_elements = [4, 5, 6]
existing_set.update(new_elements)

# Printing the result
print(existing_set)

Result:

{1, 2, 3, 4, 5, 6}

Other set operators

Union (|)

Returns a new set containing all unique elements from both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Getting all the unique elements from both sets
union_set = set1 | set2 

# Printing the result
print(union_set)

Result:

{1, 2, 3, 4, 5}

Intersection (&)

Returns a new set containing common elements between two sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Getting the common elements of both sets 
intersection_set = set1 & set2

# Printing the result
print(intersection_set)

Result:

{3}

Difference (-)

Returns a new set containing elements present in the first set but not in the second set.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Getting the elements from the first set that are not in the second set
difference_set = set1 - set2

# Printing the result
print(difference_set)

Result:

{1, 2}

Symmetric Difference (^)

Returns a new set containing elements that are unique to each set.


set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Getting the elements that are unique to each set

symmetric_difference_set = set1 ^ set2

# Printing the result
print(symmetric_difference_set)

Result:

{1, 2}

Note: instead of ^ you can also use the symmetric_difference() function that does the same

Other articles