Python set add() Method

set.add(element) adds one hashable element to a Python set and returns None. Because a set is mutable, the original set is changed in place. If the value is already present, the set remains unchanged.

Use update() when you want to add the individual elements from one or more iterables. Use add() when the value itself should become one set element.

Set order: sets are unordered. Whenever an example prints a set, the displayed element order can differ between runs or Python environments.
my_set={'Alex','Ronald','John'}
my_set.add('Lewis')
print(my_set)
Output is here
{'Ronald', 'John', 'Alex', 'Lewis'}
Adding duplicate value
my_set={'a','b','c','x'}
my_set.add('c')
print(my_set)
Output
{'a', 'x', 'c', 'b'}
Adding a value that is already present does not raise an error; a set stores each distinct value only once.

add() to build a unique list of items dynamically from user inputs or data streams.
unique_items = set()
for item in ['apple', 'banana', 'apple', 'orange']:
    unique_items.add(item)
print(unique_items)  # Output: {'banana', 'apple', 'orange'}

Comparison of add() vs update() in Python Sets

add(): Adds a single element to the set.

my_set = {1, 2}
my_set.add(3)
print(my_set)  # Output: {1, 2, 3}

update(): Adds multiple elements from an iterable (e.g., list, tuple).

my_set = {1, 2}
my_set.update([3, 4, 5])  # Adds multiple elements
print(my_set)  # Output: {1, 2, 3, 4, 5}

The add() method adds one element at a time, while update() can add several elements in one go.


Read more on update() method here.

Example 1: Adding Items Conditionally

my_set = {1, 2, 3}

# Add item only if it is greater than 10
item = 15
if item > 10:
    my_set.add(item)

print(my_set)
{1, 2, 3, 15}

Example 2: Using a Set to Track Unique Items

Sets are great for tracking unique items in a collection. For instance, let’s collect unique words from a list.

words = ["apple", "banana", "apple", "cherry", "banana", "date"]
unique_words = set()

for word in words:
    unique_words.add(word)

print(unique_words)
{'apple', 'banana', 'cherry', 'date'}

Example 3: Combining Sets with Union

We can use the union method or the | operator to combine sets.

set_a = {1, 2, 3}
set_b = {3, 4, 5}

# Combine sets
combined_set = set_a | set_b

print(combined_set)
{1, 2, 3, 4, 5}

Use Case: Tracking Unique User Visits

Sets can be used to track unique users visiting a website or application:

visits = ["user1", "user2", "user1", "user3", "user2"]
unique_visits = set(visits)

print("Unique Users:", unique_visits)
Unique Users: {'user1', 'user2', 'user3'}

Use Case: Removing Duplicates from a List

Sets can remove duplicates from a list. Use sorted() when the example also needs predictable output order.

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = sorted(set(numbers))

print(unique_numbers)
[1, 2, 3, 4, 5]

Example 4: Adding Nested Sets (frozenset)

Python sets cannot directly include other sets because they are mutable. However, we can use frozenset, which is an immutable version of a set, for this purpose:

inner_set = frozenset({1, 2, 3})
outer_set = {4, 5}

# Adding frozenset
outer_set.add(inner_set)

print(outer_set)
{frozenset({1, 2, 3}), 4, 5}

All Set Methods Questions with solutions on set


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer