A Set in Python is a collection data type used to store unique values. Sets are useful when you want to avoid duplicate items and perform fast membership testing.
Although sets are unordered, they are mutable, which means you can add and remove items after the set has been created.
In this tutorial, you will learn:
- How to add items to a set
- How to add multiple items
- The difference between
add()andupdate() - Adding items from lists, tuples, and other sets
- Common mistakes and best practices
- Real-world examples
Adding Items to a Set
Python provides the add() method to insert a new item into a set.
Syntax
set_name.add(item)Example
fruits = {
"apple",
"banana"
}
fruits.add("orange")
print(fruits)Output
{'apple', 'banana', 'orange'}Since sets are unordered, the displayed order may vary.
Add a Single Number
numbers = {
10,
20,
30
}
numbers.add(40)
print(numbers)Output
{40, 10, 20, 30}The order is not guaranteed.
Adding Duplicate Values
Sets do not allow duplicates.
Example
fruits = {
"apple",
"banana"
}
fruits.add("apple")
print(fruits)Output
{'apple', 'banana'}The duplicate value is ignored automatically.
Check Set Length Before and After Adding
fruits = {
"apple",
"banana"
}
print(len(fruits))
fruits.add("orange")
print(len(fruits))Output
2
3Adding Multiple Items
To add multiple items at once, use the update() method.
Syntax
set_name.update(iterable)An iterable can be:
- List
- Tuple
- Set
- String
Add Multiple Values Using a List
fruits = {
"apple",
"banana"
}
fruits.update([
"orange",
"mango",
"grapes"
])
print(fruits)Output
{
'apple',
'banana',
'orange',
'mango',
'grapes'
}Add Multiple Values Using a Tuple
fruits = {
"apple"
}
fruits.update(
("banana", "orange")
)
print(fruits)Output
{'apple', 'banana', 'orange'}Add Multiple Values Using Another Set
set1 = {
1, 2, 3
}
set2 = {
4, 5, 6
}
set1.update(set2)
print(set1)Output
{1, 2, 3, 4, 5, 6}Add a String Using update()
When a string is used with update(), each character is added separately.
letters = {"A"}
letters.update("BCD")
print(letters)Output
{'A', 'B', 'C', 'D'}Difference Between add() and update()
| Method | Purpose |
|---|---|
| add() | Adds one item |
| update() | Adds multiple items |
Example
Using add():
fruits = {"apple"}
fruits.add("banana")Using update():
fruits = {"apple"}
fruits.update(
["banana", "orange"]
)Adding Mixed Data Types
Python sets can contain different data types.
data = {
"Python",
100
}
data.add(3.14)
print(data)Output
{'Python', 100, 3.14}Adding Boolean Values
data = {
"Python",
100
}
data.add(True)
print(data)Output
{'Python', 100, True}Real-World Example: User Roles
roles = {
"Admin",
"Editor"
}
roles.add("Viewer")
print(roles)Output
{'Admin', 'Editor', 'Viewer'}This is useful for permission management systems.
Real-World Example: Collect Unique Tags
tags = {
"python",
"coding"
}
new_tags = [
"tutorial",
"python",
"beginner"
]
tags.update(new_tags)
print(tags)Output
{
'python',
'coding',
'tutorial',
'beginner'
}Duplicate values are automatically removed.
Using a Loop to Add Items
numbers = set()
for num in range(1, 6):
numbers.add(num)
print(numbers)Output
{1, 2, 3, 4, 5}Creating an Empty Set
To add items later, create an empty set first.
Correct
fruits = set()Incorrect
fruits = {}The second example creates a dictionary, not a set.
Adding Items to an Empty Set
fruits = set()
fruits.add("apple")
fruits.add("banana")
print(fruits)Output
{'apple', 'banana'}Common Mistakes
Mistake 1: Using append()
Incorrect:
fruits = {
"apple"
}
fruits.append("banana")Output:
AttributeErrorSets do not support append().
Correct:
fruits.add("banana")Mistake 2: Expecting Duplicates
numbers = {
1,
2
}
numbers.add(2)
print(numbers)Output:
{1, 2}The duplicate value is ignored.
Mistake 3: Using {} for Empty Set
Incorrect:
myset = {}This creates a dictionary.
Correct:
myset = set()Performance Benefits
Adding items to a set is generally very fast because sets use a hash table internally.
users = set()
users.add("alice")
users.add("bob")
users.add("charlie")This makes sets ideal for:
- User management
- Unique records
- Fast lookups
- Removing duplicates
Best Practices
Use add() for Single Items
fruits.add("orange")Use update() for Multiple Items
fruits.update(
["mango", "grapes"]
)Use Sets for Unique Data
emails = set()Avoid Duplicate Checks
Sets handle duplicates automatically.
emails.add("user@gmail.com")Quick Summary
| Task | Method |
| Add one item | add() |
| Add many items | update() |
| Add another set | update() |
| Add tuple values | update() |
| Add list values | update() |
| Create empty set | set() |
| Allow duplicates | No |
Conclusion
Adding items to a Python set is simple and efficient. The add() method is used for inserting a single value, while update() allows you to add multiple values from lists, tuples, sets, or other iterables.
Because sets automatically remove duplicates and provide fast insertion performance, they are widely used in real-world Python applications such as user management, data processing, filtering unique values, and membership testing.
Mastering add() and update() is an important step toward becoming proficient with Python sets.


0 Comments