Python Add items to Set


Add items to Set

As sets are unchangeable, so once a set is created, we cannot change any item, but python allows us to add items to set or remove items from the items.

Points to Remember:

  1. If an element already exists in the set, none of the approach will add a duplicate item.
  2. Sets are unordered, so the order in which the items are added to the set can be different from the order in which the items are stored in the set and printed.

In this article, we will discuss different ways to add items to Python sets.

Add items to Set

The add() Method – Add items

The add() Method can be used to add an item to the set. It accepts a single immutable element as the argument and adds it to the set. Note: that element should be immutable.

We can pass an immutable element like a tuple to the add() method, but, we cannot pass a mutable element like a list to the add method. On adding a string, it will be added as it is.

Adding an element. Element is added as a string.

In [1]:
birds_set = {"Eagle", "Parrot", "Owl"}
birds_set.add("Crow")

print(birds_set)

Adding a Tuple.

In [2]:
birds_set = {"Eagle", "Parrot", "Owl"}
birds_set.add(("Crow", "Pigeon"))

print(birds_set)

Adding a List.

In [3]:
birds_set = {"Eagle", "Parrot", "Owl"}
birds_set.add(["Crow", "Pigeon"])

print(birds_set)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-215a834674d7> in <module>
      1 birds_set = {"Eagle", "Parrot", "Owl"}
----> 2 birds_set.add(["Crow", "Pigeon"])
      3 
      4 print(birds_set)

TypeError: unhashable type: 'list'

The add() Method – with for loop

The add() methods can be combined with the power of the for loop. This enables us to add elements from the iterable, like a list, which cannot be directly passed to the add() method.

In [1]:
birds_set = {"Eagle", "Parrot", "Owl"}

birds_list = ["Crow", "Pigeon"]

for bird in birds_list:
    birds_set.add(bird)

print(birds_set)
{'Pigeon', 'Owl', 'Crow', 'Eagle', 'Parrot'}

The update() Method – Add Set or any Iterable

The update() Method can be used to add elements of any iterable to a set in a single operation. This method accepts a single element or the iterable sequence as the argument. Here, we can directly pass the list as an argument.

Adding a list to a set.

In [1]:
birds_set = {"Eagle", "Parrot", "Owl"}
birds_list = ["Crow", "Duck"]

birds_set.update(birds_list)
print(birds_set)
{'Crow', 'Parrot', 'Duck', 'Owl', 'Eagle'}

Adding a tuple to a set.

In [2]:
birds_set = {"Eagle", "Parrot", "Owl"}
birds_tuple = ("Crow", "Duck")

birds_set.update(birds_tuple)
print(birds_set)
{'Crow', 'Parrot', 'Duck', 'Owl', 'Eagle'}

Working with Strings

On passing a string to the update() method, it will break the sting and consider each character of the string as a single element. Look at the example below. To resolve this issue, we have a workaround, add the square brackets around the string. Now this string is considered as the list.

Update with String.

In [1]:
birds_set = {"Eagle", "Parrot", "Owl"}
bird_string = "Crow"

birds_set.update(bird_string)
print(birds_set)
{'Parrot', 'Eagle', 'w', 'Owl', 'C', 'o', 'r'}

Update with String.

In [2]:
birds_set = {"Eagle", "Parrot", "Owl"}
bird_list = ["Crow"]

birds_set.update(bird_list)
print(birds_set)
{'Parrot', 'Eagle', 'Crow', 'Owl'}

Add items from Multiple iterable

The update() method accepts multiple iterables as the comma-separated list. It will add elements from all these iterables to the set.

In [1]:
birds_set = {"Eagle", "Parrot", "Owl"}

birds_list1 = ["Crow", "Pigeon"]
birds_list2 = ["Hawk", "Lark"]
birds_list3 = ["Kiwi", "Kite"]

birds_set.update(birds_list1, birds_list2, birds_list3)

print(birds_set)
{'Kiwi', 'Parrot', 'Kite', 'Hawk', 'Owl', 'Eagle', 'Pigeon', 'Lark', 'Crow'}

The union() Method

The union method is used to combine the elements of the sets. It returns the element of set a and set b. Instead of passing a set tot the union method, we can also any other iterable like list. This is also one of the ways to add elements to the set.

In [1]:
birds_set = {"Eagle", "Parrot", "Owl"}

birds_list1 = ["Crow", "Pigeon"]

birds_set = birds_set.union(birds_list1)

print(birds_set)

Using the | operator

The | operator is another way for the union operation. It will combine the elements from the sets. In this case, it did not work directly with the list. The list has to be converted to the set.

In [1]:
birds_set = {"Eagle", "Parrot", "Owl"}

birds_list1 = ["Crow", "Pigeon"]

birds_set = birds_set | set(birds_list1)

print(birds_set)
{'Crow', 'Pigeon', 'Owl', 'Eagle', 'Parrot'}

Using the Unpacking

This approach is similar to the | operator approach, but just that, we will unpack the iterable using the asterisk approach.

In [1]:
birds_set = {"Eagle", "Parrot", "Owl"}

birds_list1 = ["Crow", "Pigeon"]

birds_set = birds_set | {*birds_list1}

print(birds_set)
{'Pigeon', 'Owl', 'Eagle', 'Parrot', 'Crow'}

Update Set Items

As sets are immutable, so once the set it created, we cannot change the individual value of the set.

References

  1. How to Add Elements to Set in Python
  2. Python – Add Set Items