I was working on a Python project where I needed to manage a large collection of unique usernames for a U.S.-based web application. I realized that sets in Python were the perfect data structure for this task because they automatically ensure all elements remain unique.
However, I found that many beginners get confused about how to add items to a set in Python, especially when deciding between the add() and update() methods. So, in this tutorial, I’ll share everything I’ve learned over the years about adding elements to a Python set, including practical code examples and my best practices from real-world projects.
Whether you’re building a data-cleaning script, managing user IDs, or working on a machine learning pipeline, understanding how to manipulate sets efficiently will make your code cleaner and faster.
What Is a Set in Python?
Before we get into adding elements, let’s quickly understand what a Python set is.
A set in Python is an unordered collection of unique elements. This means that if you try to add a duplicate value, Python simply ignores it. Sets are also mutable, so you can add or remove items anytime after creation.
Here’s a simple example of a Python set:
# Creating a Python set of U.S. states
us_states = {"California", "Texas", "New York"}
print(us_states)When you run this code, you’ll see that the items may not appear in the same order you entered them. That’s because sets in Python are unordered collections.
Different Ways to Add Items to a Set in Python
There are two main ways to add items to a set in Python:
- Using the add() method – for adding a single item.
- Using the update() method – for adding multiple items.
Let’s look at both methods in detail.
Method 1 – Add a Single Item Using the add() Method
When you need to insert just one new element into a Python set, the simplest way is to use the add() method.
Here’s how it works:
# Example: Adding a single item to a Python set
us_states = {"California", "Texas", "New York"}
# Adding a new state
us_states.add("Florida")
print(us_states)You can refer to the screenshot below to see the output.

In this example, I started with a set of three U.S. states and then added “Florida” using the add() method. When you print the set, you’ll see the new state included in the output.
If “Florida” already existed in the set, Python would simply ignore it, no error, no duplicates. That’s one of the biggest advantages of using sets.
Method 2 – Add Multiple Items Using Python’s update() Method
When you want to add multiple elements at once, the update() method is the right choice. It can accept any iterable, such as a list, a tuple, or even another set.
Here’s an example:
# Example: Adding multiple items to a Python set
us_states = {"California", "Texas", "New York"}
# Adding multiple new states
us_states.update(["Florida", "Illinois", "Ohio"])
print(us_states)You can refer to the screenshot below to see the output.

In this example, I added three new states using update(). This method is especially useful when you’re merging data from multiple sources, for example, combining user IDs from different databases.
Method 3 – Add Items from Another Python Set
You can also use the update() method to combine two sets. This is a common pattern when you’re merging datasets.
# Example: Merging two sets in Python
east_coast_states = {"New York", "New Jersey", "Massachusetts"}
west_coast_states = {"California", "Oregon", "Washington"}
# Combine both sets
east_coast_states.update(west_coast_states)
print(east_coast_states)You can refer to the screenshot below to see the output.

This code merges the two sets, and the final set contains all unique states from both coasts. It’s a clean and efficient way to combine data without duplicates.
Method 4 – Add Items Using a Loop in Python
Sometimes, you may want to add items to a set dynamically, for example, by reading data from a file or user input. In such cases, you can use a for loop with the add() method.
Here’s how:
# Example: Adding items to a set using a loop
unique_zip_codes = set()
# Simulating user input or file data
zip_list = ["10001", "30301", "60601", "10001", "94102"]
for zip_code in zip_list:
unique_zip_codes.add(zip_code)
print(unique_zip_codes)You can refer to the screenshot below to see the output.

This approach ensures that even if there are duplicate ZIP codes, the set only keeps unique ones. I often use this method when processing large lists of data in my Python scripts.
Add Immutable Items Only
One important thing to remember: Python sets can only contain immutable (hashable) items. This means you can add strings, numbers, or tuples, but not lists or dictionaries.
If you try to add a list, Python will raise a TypeError.
Example:
# Example: Trying to add a list to a set (will cause an error)
my_set = {1, 2, 3}
my_set.add([4, 5, 6]) # TypeError: unhashable type: 'list'To fix this, you can convert the list to a tuple before adding it:
# Correct way: Convert list to tuple before adding
my_set = {1, 2, 3}
my_set.add(tuple([4, 5, 6]))
print(my_set)This is a small detail, but it’s one that every Python developer should know.
Real-World Example: Manage Unique Usernames in Python
Let’s look at a more realistic example. Imagine you’re building a web application in the U.S. that collects usernames, and you want to ensure no duplicates are stored.
Here’s how you can do it using a Python set:
# Example: Managing unique usernames using a Python set
usernames = {"alex", "maria", "john"}
# New usernames coming from registration form
new_usernames = ["maria", "chris", "alex", "susan"]
# Add new usernames to the set
for name in new_usernames:
usernames.add(name)
print("Unique usernames:", usernames)When you run this code, you’ll see that “maria” and “alex” are not duplicated. The set automatically takes care of it.
This technique is extremely useful in real-world applications like managing email lists, user IDs, or product SKUs.
Check If an Item Exists Before Adding
Although sets automatically handle duplicates, sometimes you might want to check explicitly if an item exists before adding it. This can be useful for logging or debugging.
Here’s an example:
# Example: Checking existence before adding
cities = {"New York", "Los Angeles", "Chicago"}
new_city = "Chicago"
if new_city in cities:
print(f"{new_city} already exists in the set.")
else:
cities.add(new_city)
print(f"{new_city} added to the set.")
print(cities)This approach gives you more control, especially when you want to track duplicate entries.
Common Mistakes When Adding Items to a Set
Over the years, I’ve seen beginners make a few common mistakes when working with Python sets:
- Using append() instead of add() – append() works with lists, not sets.
- Trying to add unhashable items – like lists or dictionaries.
- Expecting order – sets don’t maintain order, so don’t rely on item positions.
Avoiding these mistakes will save you a lot of debugging time.
Best Practices for Adding Items to a Set in Python
Here are a few best practices I follow:
- Use add() for a single item and update() for multiple items.
- Always ensure the data type is immutable before adding.
- Use sets when you need uniqueness and fast membership checks.
- Convert lists to sets to remove duplicates quickly.
These small habits can make your Python code more efficient and readable.
So that’s how I add items to a set in Python, whether it’s one item or many, using add() or update(), or even merging entire datasets. Sets are one of my favorite Python data structures because of their simplicity and performance.
Next time you’re working on a project that requires unique values, like managing U.S. ZIP codes, usernames, or product IDs, try using Python sets. You’ll find that adding and managing data becomes faster, cleaner, and more reliable.
You can also read other Python articles:
- Create a CSV File in Python
- Write Bytes to a File in Python
- Read Large CSV Files in Python
- Create a Python File in Terminal

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.