Add Set Items
2 min read ·
Sets in Python are mutable, which means you can add new elements after the set is created.
However, sets only store unique values, so duplicates are ignored automatically.
This topic covers all valid ways to add items to a set, along with important rules and edge cases.
Add One Item Using add()
The
add() method adds a single element to the set.Syntax
Example
Adding a duplicate:
The set remains unchanged.
Add Multiple Items Using update()
The
update() method adds multiple elements from an iterable.Syntax
Example with List
Example with Tuple
Example with Another Set
Add Characters from String (Important)
Each character is added separately.
Add Items Conditionally
Add Items Using Loop
Add Items to Frozen Set (Not Allowed)
Frozen sets are immutable.
Common Mistakes
Using append() on a Set
Sets do not support
append().Adding Mutable Items
Only hashable items are allowed.
Important Rules
- Duplicate values are ignored
- Order is not preserved
- Only immutable items can be added
add()→ single itemupdate()→ multiple items