The basic way to add a single item to a set

Use the add() method to put one item into a set. Call it on the set, pass the item in parentheses, and Python adds it immediately.

Here is the pattern:

my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}

The add() method changes the set in place — it does not return a new set. If you add an item that already exists in the set, nothing happens. Sets automatically reject duplicates, so my_set.add(2) when 2 is already there makes no change.

Key Takeaways

  • Use add() to insert a single item: my_set.add(value) modifies the set directly.
  • Use update() to add multiple items at once from a list, tuple, or another set: my_set.update([4, 5, 6]).
  • Sets automatically ignore duplicates, so adding an item that already exists has no effect.
  • The union operator | creates a new set combining two sets without changing either original: new_set = my_set | {7, 8}.

Adding multiple items at once with update()

When you need to add several items, use update() instead of calling add() over and over. Pass it a list, tuple, another set, or any iterable, and update() adds all of them in one call.

my_set = {1, 2, 3} my_set.update([4, 5, 6]) print(my_set) # Output: {1, 2, 3, 4, 5, 6}

You can also pass multiple iterables separated by commas:

my_set.update([4, 5], [6, 7])

Like add(), update() modifies the set in place and ignores any duplicates. If you pass [2, 4, 5] and 2 already exists, it skips the 2 and adds only 4 and 5.

Combining two sets without changing the originals

If you want to merge sets but keep the originals unchanged, use the union operator | or the union() method. Both create a new set containing all items from both sets.

set_a = {1, 2, 3} set_b = {3, 4, 5} set_c = set_a | set_b print(set_c) # Output: {1, 2, 3, 4, 5}

The union() method does the same thing:

set_c = set_a.union(set_b)

Notice that 3 appears only once in the result, even though it was in both sets. This is the set behavior — duplicates are automatically removed. The original sets set_a and set_b remain unchanged.

The difference between add(), update(), and union()

MethodWhat it takesChanges the original set?Use when
add()One itemYesAdding a single value
update()Multiple items (list, tuple, set, etc.)YesAdding many items at once
union() or |Another setNo — creates a new setCombining sets without modifying either

Adding items from a dictionary or string

You can add items from any iterable. If you pass a dictionary, update() adds only the keys, not the values.

my_set = {1, 2} my_dict = {'a': 10, 'b': 20} my_set.update(my_dict) print(my_set) # Output: {1, 2, 'a', 'b'}

If you pass a string, update() treats each character as a separate item:

my_set = {1, 2} my_set.update("abc") print(my_set) # Output: {1, 2, 'a', 'b', 'c'}

This works because strings are iterable — Python loops through each character. If you want to add the entire string as a single item, use add() instead: my_set.add("abc") adds the string "abc" as one element.

What happens when you try to add an unhashable type

Sets can only hold hashable types — items that cannot change, like integers, strings, tuples, and frozensets. If you try to add a list or dictionary, Python raises a TypeError.

my_set = {1, 2} my_set.add([3, 4]) # TypeError: unhashable type: 'list'

This is a core rule of sets: every item must be immutable. If you need to store a collection inside a set, convert it to a tuple or frozenset first:

my_set.add((3, 4)) # Works fine my_set.add(frozenset([5, 6])) # Also works

Frequently Asked Questions

Can I add an item to a set inside a loop?

Yes. You can call add() or update() as many times as you need. If you are looping through a list and adding each item, use update() with the entire list instead — it is faster and cleaner than calling add() in a loop.

What is the difference between a set and a list when adding items?

Lists use append() and allow duplicates; sets use add() and reject duplicates automatically. Lists preserve order and let you access items by position; sets do not. Choose a set when you only care whether an item exists, and a list when order or duplicates matter.

Does add() return anything I can use?

No. add() and update() both return None. They modify the set in place. If you need to use the result, assign it to a variable before calling add(), or use union() to create a new set instead.

Can I add items to a set while looping through it?

No. Modifying a set while iterating over it raises a RuntimeError. If you need to add items based on what you find, collect them in a separate list first, then use update() after the loop ends.