
When you work with sets in Python, one of the most common operations is adding new items. Python provides two main methods to add set items — the add() method for inserting a single element and the update() method for adding multiple elements at once. Understanding how to add items to a set in Python is essential because sets are one of the four built-in collection data types, and they are widely used when you need to store unique values without duplicates.
Sets are mutable, which means you can add items to a set after it has been created. However, the items themselves must be immutable and hashable. This means you can add strings, numbers, and tuples to a set, but you cannot add lists or dictionaries. Let us explore every way to add set items in Python with practical examples.
The most straightforward way to add items to a set in Python is by using the add() method. This method takes exactly one argument and adds it to the set. If the item already exists in the set, the set remains unchanged because sets do not allow duplicate values.
Here is how to add a single element to a set:
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)
Output
{'cherry', 'banana', 'orange', 'apple'}
The add() method modified the original set by inserting the new item. Notice that the order of elements in the output may differ from what you expect because sets are unordered collections in Python.
Let us see what happens when you try to add an item that already exists in the set:
fruits = {"apple", "banana", "cherry"}
fruits.add("banana")
print(fruits)
Output
{'cherry', 'banana', 'apple'}
The set stays exactly the same. The add() method does not raise an error or create a duplicate — it simply ignores the operation when the item is already present. This behavior makes Python sets perfect for maintaining collections of unique values.
You can add items of various data types to a set in Python, as long as the items are hashable. Numbers, strings, booleans, and tuples are all valid items you can add to a set.
Here is an example of adding different data types:
my_set = {1, 2, 3}
my_set.add("hello")
print(my_set)
my_set.add(3.14)
print(my_set)
my_set.add(True)
print(my_set)
Output
{1, 2, 3, 'hello'}
{1, 2, 3, 3.14, 'hello'}
{1, 2, 3, 3.14, 'hello'}
Notice something interesting in the last print statement. Adding True did not change the set because Python considers True and 1 to be equal values. Since 1 is already in the set, True is treated as a duplicate and is not added.
You can also add a tuple to a set because tuples are immutable and hashable:
my_set = {"apple", "banana"}
my_set.add((1, 2, 3))
print(my_set)
Output
{'banana', (1, 2, 3), 'apple'}
However, you cannot add a list or a dictionary to a set because they are mutable and therefore not hashable:
my_set = {"apple", "banana"}
try:
my_set.add([1, 2, 3])
except TypeError as e:
print(f"Error: {e}")
Output
Error: unhashable type: 'list'
This restriction exists because Python sets use hash values internally to store and look up elements efficiently.
When you need to add multiple items to a set in Python at once, the update() method is the right choice. Unlike add(), which takes a single element, the update() method accepts any iterable — such as a list, tuple, string, or another set — and adds all its elements to the set.
Here is how to add multiple items to a set using a list:
fruits = {"apple", "banana", "cherry"}
fruits.update(["orange", "mango", "grape"])
print(fruits)
Output
{'grape', 'cherry', 'mango', 'banana', 'orange', 'apple'}
All three new items were added to the set at once. This is much more convenient than calling add() three separate times.
You can also pass a tuple to the update() method to add set items:
colors = {"red", "green"}
colors.update(("blue", "yellow", "purple"))
print(colors)
Output
{'purple', 'green', 'blue', 'yellow', 'red'}
The update() method works with any iterable, making it extremely flexible for adding multiple elements to a set in Python.
A very common use case for the update() method is combining two sets. You can add all items from one set into another set using update():
set1 = {"apple", "banana", "cherry"}
set2 = {"orange", "mango", "grape"}
set1.update(set2)
print(set1)
Output
{'grape', 'cherry', 'mango', 'banana', 'orange', 'apple'}
When both sets contain common elements, duplicates are automatically handled. The update() method adds only the items that do not already exist in the target set:
set1 = {"apple", "banana", "cherry"}
set2 = {"banana", "mango", "cherry", "kiwi"}
set1.update(set2)
print(set1)
Output
{'cherry', 'kiwi', 'mango', 'banana', 'apple'}
Even though both sets contained "banana" and "cherry", the resulting set has no duplicates. This is one of the fundamental properties of sets in Python.
The update() method can accept more than one iterable as arguments. This allows you to add items from multiple sources to a set in a single call:
my_set = {"apple"}
my_set.update(["banana", "cherry"], ("orange", "grape"), {"mango", "kiwi"})
print(my_set)
Output
{'grape', 'cherry', 'kiwi', 'mango', 'banana', 'orange', 'apple'}
This example passed a list, a tuple, and another set all in one update() call. Every element from every iterable was added to the set. This is a powerful way to merge multiple collections of items into a single set in Python.
When you pass a string to the update() method, Python treats the string as an iterable of individual characters. Each character gets added as a separate element to the set:
my_set = {"a", "b"}
my_set.update("hello")
print(my_set)
Output
{'h', 'l', 'a', 'o', 'b', 'e'}
The string "hello" was broken down into individual characters, and each unique character was added to the set. Notice that only one "l" appears even though "hello" contains two, because sets eliminate duplicates automatically.
If you want to add the entire string as a single item rather than splitting it into characters, use the add() method instead:
my_set = {"a", "b"}
my_set.add("hello")
print(my_set)
Output
{'hello', 'a', 'b'}
This is an important distinction to remember when you add set items in Python. The add() method treats the argument as a single element, while update() iterates over it.
Python 3.9 and later versions introduced the union operator |= as an alternative way to add items to a set. The |= operator works similarly to update() when used with another set:
fruits = {"apple", "banana"}
tropical = {"mango", "pineapple"}
fruits |= tropical
print(fruits)
Output
{'pineapple', 'banana', 'mango', 'apple'}
The |= operator merges the second set into the first one, which is functionally identical to calling update() with a set argument. This provides a cleaner syntax for combining sets.
You can also use the | operator to create a new set without modifying the originals:
fruits = {"apple", "banana"}
tropical = {"mango", "pineapple"}
all_fruits = fruits | tropical
print(all_fruits)
print(fruits)
Output
{'pineapple', 'banana', 'mango', 'apple'}
{'banana', 'apple'}
The | operator returns a new set containing all items from both sets, while the original sets remain unchanged. This is useful when you want to add set items together without modifying either source set.
Sometimes you need to add items to a set dynamically based on some logic. You can use the add() method inside a loop to add set items one at a time:
squares = set()
for num in range(1, 6):
squares.add(num ** 2)
print(squares)
Output
{1, 4, 9, 16, 25}
You can also build a set using a set comprehension, which is a more Pythonic approach:
squares = {num ** 2 for num in range(1, 6)}
print(squares)
Output
{1, 4, 9, 16, 25}
Both approaches produce the same result. Set comprehensions are generally preferred because they are more concise and readable.
Here is a complete working example that demonstrates all the ways to add items to a set in Python, including the add() method, update() method, union operator, and loop-based approaches:
grocery_set = {"milk", "eggs", "bread"}
print("Initial set:", grocery_set)
grocery_set.add("butter")
print("After add('butter'):", grocery_set)
grocery_set.add("milk")
print("After add('milk') again:", grocery_set)
grocery_set.update(["cheese", "yogurt", "cream"])
print("After update with list:", grocery_set)
more_items = {"flour", "sugar"}
grocery_set.update(more_items)
print("After update with set:", grocery_set)
grocery_set.update(["pasta", "rice"], ("salt", "pepper"))
print("After update with multiple iterables:", grocery_set)
extra = {"honey", "jam"}
grocery_set |= extra
print("After |= operator:", grocery_set)
new_set = grocery_set | {"cereal", "oats"}
print("New set with | operator:", new_set)
print("Original set unchanged:", grocery_set)
even_squares = set()
for num in range(2, 12, 2):
even_squares.add(num ** 2)
print("Even squares via loop:", even_squares)
odd_cubes = {num ** 3 for num in range(1, 8, 2)}
print("Odd cubes via comprehension:", odd_cubes)
mixed_set = set()
mixed_set.add(42)
mixed_set.add("python")
mixed_set.add((1, 2, 3))
mixed_set.add(3.14)
print("Mixed data types set:", mixed_set)
print("Total grocery items:", len(grocery_set))
Output
Initial set: {'bread', 'eggs', 'milk'}
After add('butter'): {'bread', 'butter', 'eggs', 'milk'}
After add('milk') again: {'bread', 'butter', 'eggs', 'milk'}
After update with list: {'bread', 'cream', 'butter', 'yogurt', 'cheese', 'eggs', 'milk'}
After update with set: {'bread', 'cream', 'butter', 'yogurt', 'flour', 'cheese', 'sugar', 'eggs', 'milk'}
After update with multiple iterables: {'bread', 'cream', 'rice', 'butter', 'yogurt', 'pepper', 'flour', 'cheese', 'sugar', 'eggs', 'pasta', 'salt', 'milk'}
After |= operator: {'bread', 'cream', 'rice', 'butter', 'yogurt', 'pepper', 'flour', 'cheese', 'sugar', 'eggs', 'jam', 'honey', 'pasta', 'salt', 'milk'}
New set with | operator: {'bread', 'cream', 'rice', 'butter', 'yogurt', 'pepper', 'flour', 'cheese', 'sugar', 'eggs', 'jam', 'cereal', 'honey', 'oats', 'pasta', 'salt', 'milk'}
Original set unchanged: {'bread', 'cream', 'rice', 'butter', 'yogurt', 'pepper', 'flour', 'cheese', 'sugar', 'eggs', 'jam', 'honey', 'pasta', 'salt', 'milk'}
Even squares via loop: {4, 36, 100, 16, 64}
Odd cubes via comprehension: {1, 343, 27, 125}
Mixed data types set: {42, 3.14, (1, 2, 3), 'python'}
Total grocery items: 15