This post will discuss how to add values to a set in Python.

1. Add single element

To add an element to a set, the standard function is add(). The following example demonstrates this usage of add().

Download  Run Code

2. Add contents of another Iterable

If you need to add other iterable contents, you can use the update() function. The following code demonstrates how to add the contents of a list to a set.

Download  Run Code

3. Add another Set

You can also use the | operator (or |= operator) to concatenate two sets. Note this returns a new set, as shown below:

Download  Run Code

 
You can also use the union() function to merge the contents of two sets and return a new set.

Download  Run Code

That’s all about adding values to a set in Python.