Skip to Content

Add items to a Set in Python

In this tutorial, we will look at how to add elements to set in Python with the help of some examples. We will also look at how we can add all items of an iterable to a set.

diagram showing element added to a set

Python implements a set data structure which comes with a number of useful functions for common set operations. You can use the set add() function to add individual items to a set or the set update() function to add items from an iterable to a set. The following is the syntax:

# add individual item to a set
a.add(item)
# add items from an iterable to a set
a.update(iterable)

Let’s look at some examples to clearly understand the difference between the two methods.

To add individual elements to a set, pass the element to be added as an argument to the set add() function. For example,

# create a set
a = {1,2,3}
# add item using add()
a.add(4)
# display the set
print(a)

Output:

{1, 2, 3, 4}

You can see that the passed element has been added to the set.

Note that you can also use the set update() function to add individual items to a set but you need to pass them inside an iterable, for example, a list. Let’s perform the same operation as above but this time using the set update() function.

# create a set
a = {1,2,3}
# add item using update()
a.update([4])
# display the set
print(a)

Output:

{1, 2, 3, 4}

We get the same result as above. But, if you want to add individual items to a set, you should use the set add() function.

The set update() function is primarily used to add items from an iterable (for example, list, tuple, string, etc.) to the original set. Here’s an example –

# create a set
a = {1,2,3}
# add items using update()
a.update([4,5])
# display the set
print(a)

Output:

{1, 2, 3, 4, 5}

The resulting set contains each item from the passed iterable along with the original elements. You can also use a set union operation to combine two sets.

Note that there’s a difference between the set add() and update() function. Let’s understand this better with the help of an example – What would be the result if we try to add a string using the set add() and update() functions?

# create a set
a = set()
# using add()
a.add("cats and dogs")
# display the set
print(a)

# create a set
a = set()
# using update()
a.update("cats and dogs")
# display the set
print(a)

Output:

{'cats and dogs'}
{'n', 'c', ' ', 's', 'a', 'o', 't', 'g', 'd'}

You can see that the set add() function added the entire string as an element to the set whereas the set update() function added each character of the string as a separate item to the set. This happened because the set update() function is used to add items from a iterable to a set.

Also, note that you cannot use the set add() function to add mutable types like lists to a set. We were able to add a string in the above example because strings are immutable in Python. This is so because the elements of a set must be hashable.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.