Add Set Items
Learn how to add items to a Python set using add() for single elements and update() for multiple values from any iterable, with clear examples.
Python sets are unordered collections of unique elements. Once a set is created you can grow it at any time using two dedicated methods: add() for inserting a single element and update() for inserting many elements at once from any iterable. This chapter covers both methods in depth, explains when to use each one, and highlights common gotchas.
If you are new to sets, read the Python Sets chapter first. To learn how to remove items, see Remove Set Items.
The add() Method
add() inserts one element into the set. If the element already exists, the set is not changed — no error is raised and no duplicate is created.
Syntax
set.add(element)element— the value to insert. It must be hashable (strings, numbers, tuples of hashable values, etc.). Lists and dicts cannot be added.- Return value:
None. The set is modified in place.
Adding a new element
Add a string to a set
Adding a duplicate is safe
Calling add() with an element that already exists does nothing — the set stays the same size:
Add a duplicate element to a set
fruits = {'apple', 'banana', 'cherry'}
fruits.add('apple') # 'apple' is already present
print(fruits)
# {'apple', 'banana', 'cherry'} (unchanged)This is one of the most useful properties of sets: you never have to check whether an element exists before adding it.
add() only accepts one argument
add() takes exactly one argument. To insert several elements at once, use update() instead (see the next section).
# This raises TypeError: set.add() takes exactly one argument (2 given)
# fruits.add('kiwi', 'mango')add() requires a hashable element
Only hashable types can be stored in a set. Lists and dictionaries are not hashable, so passing them to add() raises a TypeError:
s = {1, 2, 3}
# s.add([4, 5]) # TypeError: unhashable type: 'list'
# Use a tuple instead:
s.add((4, 5))
print(s)
# {1, 2, 3, (4, 5)}The update() Method
update() adds all elements from one or more iterables to the set in a single call. It accepts any iterable — another set, a list, a tuple, or even a string.
Syntax
set.update(iterable1, iterable2, ...)- One or more iterables may be passed.
- Duplicates between the existing set and the new elements are silently ignored.
- Return value:
None. The set is modified in place.
Adding elements from a list
Update a set from a list
fruits = {'apple', 'banana'}
fruits.update(['cherry', 'date', 'elderberry'])
print(fruits)
# {'apple', 'banana', 'cherry', 'date', 'elderberry'}Adding elements from another set
Merge two sets with update()
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2)
print(set1)
# {1, 2, 3, 4, 5}Note that update() modifies set1 in place, whereas union() returns a new set without touching either original.
Adding from multiple iterables at once
You can pass several iterables to a single update() call:
Update a set from multiple iterables
numbers = {1, 2, 3}
numbers.update([4, 5], {5, 6, 7})
print(numbers)
# {1, 2, 3, 4, 5, 6, 7}Passing a string to update()
Because a string is iterable, update() breaks it into individual characters. This is usually not what you want, so be careful:
Update with a string — adds individual characters
s = {'hello'}
s.update('abc')
print(s)
# {'hello', 'a', 'b', 'c'} — each character becomes a separate elementTo add a whole string as one element, use add() instead.
add() vs update() — Quick Comparison
add() | update() | |
|---|---|---|
| Number of elements | One | Many |
| Argument type | A single hashable value | Any iterable (list, set, tuple, …) |
| Modifies set in place? | Yes | Yes |
| Returns | None | None |
Practical Examples
Building a tag set dynamically
A common use case is collecting unique tags or identifiers as a program runs:
Collect unique tags with a set
tags = set()
# add() for one tag at a time
tags.add('python')
tags.add('tutorial')
tags.add('python') # duplicate — ignored
print(tags)
# {'python', 'tutorial'}
# update() to add several tags at once
tags.update(['coding', 'beginner', 'tutorial'])
print(tags)
# {'python', 'tutorial', 'coding', 'beginner'}Removing duplicates while accumulating data
Because add() silently ignores duplicates, you can use a set as an accumulator to deduplicate items from multiple sources:
Deduplicate items from two lists using a set
batch1 = [101, 102, 103, 102]
batch2 = [103, 104, 105]
seen = set()
seen.update(batch1)
seen.update(batch2)
print(sorted(seen))
# [101, 102, 103, 104, 105]Key Points
- Sets store only unique elements;
add()andupdate()never create duplicates. - Sets are unordered — the insertion order is not preserved and may vary between runs.
add()inserts exactly one hashable element;update()inserts all elements from any number of iterables.- Both methods return
Noneand mutate the set in place. - To combine two sets into a new set without modifying either, use
union()(see Set Methods).