In Python, you can add new elements to a set using:
add() → to insert a single element.update() → to add multiple elements from any iterable (list, tuple, set, etc.).Sets automatically ignore duplicates and do not preserve order, so results may appear in any order.
add(item) inserts exactly one new element.update(iterable) loops through the iterable and adds each element.update().set_name.add(element)set_name.update(iterable)Internally, sets use a hash-table based structure. That is why:
x in my_set are very fast.Use the add() method to insert a single element into a set.
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits) # {'apple', 'banana', 'cherry', 'orange'}
Use the update() method to add multiple items at once. You can pass a list, tuple, or another set.
fruits = {"apple", "banana", "cherry", "orange"}
fruits.update(["kiwi", "mango"]) # list
fruits.update(("pear", "grape")) # tuple
fruits.update({"pineapple", "papaya"}) # set
print(fruits)
# {'apple', 'banana', 'cherry', 'orange', 'kiwi', 'mango', 'pear', 'grape', 'pineapple', 'papaya'}
When you print a set, Python shows all elements, but their order is not fixed. So your output may look like:
{'orange', 'papaya', 'banana', 'apple', 'cherry', 'pear', 'pineapple', 'kiwi', 'mango', 'grape'}
This is completely fine—sets are designed for fast membership checks and uniqueness, not ordering.
add() when you know you need to insert a single element.update() when you already have elements in an iterable (list, tuple, set).add().update() to add multiple items from a list.update() with a tuple and then another set to merge all items.list() and print it.