add() method for set

add(element) takes one arguments .

element : element to be added to set
Returns : No return value.
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'}
There is no error if we try to add element already available but it will not add the element again as set can't have duplicate elements.

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 Addition

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 help remove duplicates when you convert a list into a set.

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(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
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer