Sets in Python are unordered collections that only contain unique elements. The set itself is not hashable, but it must contain hashable elements.
When it comes to adding multiple elements, you cannot use a set.add() method directly because it will add one element at a time. However, you can use the for loop with the set.add() method, but that’s not an efficient approach.
We need an approach that accepts an iterable and appends it to the set. An iterable may contain multiple elements, so it will be added as a source of individual elements to expand a set.
Here are three main ways:
- Using set.update()
- Using | operator
- Using set.union()
Method 1: Using set.update()
The set.update() is the most efficient and Pythonic method that accepts any type of iterable and adds its elements to the set without throwing any errors.
main_set = {11, 21, 31} set_to_add = {41, 51, 61} main_set.update(set_to_add) print(main_set) # {51, 21, 41, 11, 61, 31}
The above output shows a set with appended elements, and the order has been changed.
Duplicate elements
If some of the iterable elements are already in the set, the .update() method will ignore them because duplicate elements are not allowed in the set.
main_set = {11, 21, 31} set_to_add = {41, 21, 31} # 21 and 31 are already in main_set main_set.update(set_to_add) print(main_set) # {21, 41, 11, 31}
Adding a list to set
We can take a list as an iterable, make it a source of multiple individual elements, and append it to a set.
main_set = {11, 21, 31} list = [41, 51, 61] # list to be added to the set main_set.update(list) print(main_set) # {41, 11, 51, 21, 61, 31}
And it works as expected! The above output shows that .update() method did not add a list as a single element.
We cannot add a list itself as an element to a set because lists are not hashable.
Adding a tuple to set
A tuple is also iterable, and you can take it as a source of multiple individual elements.
main_set = {11, 21, 31} tuple = (41, 51, 61) # tuple to be added to the set main_set.update(tuple) print(main_set) # {41, 11, 51, 21, 61, 31}
Method 2: Using | (union) operator
The “|” operator unions two sets and returns a new set containing all unique elements from both sets.
main_set = {11, 21, 31} set_to_append = {41, 51, 61} # Another set new_set = main_set | set_to_append print(new_set) # {41, 11, 51, 21, 61, 31}
Here, we get the new set in the output with combined elements.
If you want to modify the original set, you can use the |= operator.
main_set = {11, 21, 31} set_to_append = {41, 51, 61} # Another set main_set |= set_to_append print(main_set) # {41, 11, 51, 21, 61, 31}
The union operator (|) only works for sets. This means you can only combine two sets. You cannot union a set with a tuple or list directly. If you try, you’ll get a TypeError like this: TypeError: unsupported operand type(s) for |=: ‘set’ and ‘tuple’
main_set = {11, 21, 31} tuple = (41, 51, 61) main_set |= tuple print(main_set) # TypeError: unsupported operand type(s) for |=: 'set' and 'tuple'
Method 3: Using union()
The Set union() method creates a new set with combined elements rather than modifying the original set in-place. It’s a good choice for new set containing all the elements.
main_set = {11, 21, 31} set_to_append = {41, 51, 61} # Another set new_set = main_set.union(set_to_append) # Union of two sets print(new_set) # {51, 21, 41, 11, 61, 31}
Using for loop with set.add()
You can add numerous elements one by one using the for loop and set.add() function. However, it does not append all elements at once making it less efficient due to multiple function calls.
main_set = {11, 21, 31} elements_to_add = [4, 5, 6] # List for element in elements_to_add: main_set.add(element) print(main_set) # {4, 5, 6, 11, 21, 31}
You can also use list comprehension and the functools.reduce() method, but there is no need to cover all these ways because what we have covered is enough.