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.
How to add element to a set in Python?
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.
1. Add individual element to a set
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
{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.
2. Add elements from a iterable to a set
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.