In this Python tutorial, we will learn how to add elements in a set in Python. We’ll use some built-in functions to understand various approaches to add items in a set in Python.
As a Developer, while making the Python Project I got the requirement to add elements to a set in Python.
Here we will see:
- How to Add Elements in a Set in Python using add()
- How to Add Elements in a Set in Python using update()
- How to Add Elements in a Set in Python using union()
- How to Add Elements in a Set in Python using | operator
- How to Add Elements in a Set in Python using for loop and add function
How to Add Elements in a Set in Python
Indexing serves no purpose since the sets are not ordered. A set item cannot be modified or retrieved using indexing. The set data structure doesn’t take care of it. One item is added using the add() function, and several things are added using the update() function.
In Python, there are numerous methods for adding elements in a Python set. We’ll go through how to Add Elements in a Set in Python.
How to Add Elements in a Set in Python using add()
- In this section, we will discuss How to Add Elements in a Set in Python using add().
- An element can be added to the set using this built-in function set. Only one item is added to the set at a time by this function. If we attempt to add an element that already exists in the set, it will not be added because the set only contains unique elements; instead, it will continue to use the original set and run the program.
- To add an element, a parameter is required, and by default, it takes none value.
Syntax
Here is the Syntax of the set add() function in Python.
set.add(element)
Example:
Let’s take an example and check how to Add Elements in a Set in Python using add().
Source Code:
Country_name = {'U.S.A', 'Australia', 'Germany'}
#add() method in Python
Country_name.add('China')
# Display the Content
print("Adding items into set :",Country_name)
In the following given code, we declared the input set named ‘Country_name’. Now we add the new element to the input set by using the set.add() function in Python.
Here is the implementation of the following given code.
This is how to Add Elements in a Set in Python using add().
Read: Python program to create empty set
How to Add Elements in a Set in Python using update()
- Now let us understand how to Add Elements in a Set in Python using update().
- Adding elements to a set in Python is done using a built-in method. This function does element addition on a single line. It is more effective and quicker than others. If the user wants to add numerous items at once, this method is helpful.
Syntax:
Let’s have a look at the Syntax and understand the working of the update() function in Python.
set.update(iterable)
This parameter takes only one parameter and it specifies the iterable object like a set or list.
Example:
Here we will take an example and check how to Add Elements in a Set in Python using update().
Source Code:
#input set
cars_name = {'BMW', 'FORD-150', 'Volkswagen'}
# a list of numbers to add
add_new_cars = ['Audii']
# add all elements of list to the set
cars_name.update(add_new_cars)
print('Modified set after adding items: ', cars_name)
In the above code, we declared the given set, and then we added a new item into the set by using the set.update() function.
You can refer to the below Screenshot
As you can see in the Screenshot we have discussed how to Add Elements in a Set in Python using update().
Read: Union of sets Python + Examples
How to Add Elements in a Set in Python using union()
- In this section, we will discuss how to Add Elements in a Set in Python using union().
- All of the items from both the input set and the given set are included in the set that the union() function produces. A comma should be used to separate each collection you describe.
Syntax:
Here is the Syntax of the union() method in Python
set1.union(iterable)
Note: This parameter takes only one parameter that is iterable and the iterables must be a set.
Example:
Let’s take an example and check how to Add Elements in a Set in Python using union().
Source Code:
#Input set
random_numbers= {762, 845, 198, 267, 945}
sample_added_num = {563, 345}
#Added items in a list
new_output = random_numbers.union(set(sample_added_num))
#updated set
print('Modified Set: ', new_output)
In the following given code first, we will take random numbers in the input set and then add the elements in the given set by using the union() function.
Here is the implementation of the following given code.
This is how to Add Elements in a Set in Python using union().
Read: Python program for intersection of sets
How to Add Elements in a Set in Python using | operator
- Here we will discuss how to Add items in a Set in Python using the | operator.
- It resembles the union. We make a union of the two sets after converting the list to a set. But we used the set() function to change our list into a set.
Example:
Here we will take an example and check how to Add Elements in a Set in Python using | operator.
Source code:
new_val = {67, 78, 167, 90, 65}
new_added_values = [6, 7]
# Added elements into a set
new_val |= set(new_added_values)
#updated set
print('Modified Set: ', new_val)
In the above code first, we will define the input set named ‘new_val’ and then declared a new variable ‘new_added_values’. Next, we added the items into a set by using the | operator.
Here is the Screenshot of the following given code.
In this example, we have understood how to Add Elements in a Set in Python using the | operator.
Read: Python Dictionary of sets
How to Add Elements in a Set in Python using for loop and add function
With each iteration of the for loop, we may add elements to the set by passing each item as an input to the add() function. Each element is added to the set by the add() method, which prints the modified set.
Example:
input_val = {78, 12, 96, 83, 18}
new_added_val = [17, 89]
# By using the for loop
for i in new_added_val :
# add each element to the set
input_val.add(i)
#prints updated set
print('Modified Set after addition: ', input_val)
Here is the Screenshot of the following given code.
This is how to Add Elements in a Set in Python using for loop and add function
You may also like to read the following Python tutorials.
- Python dictionary multiple values
- Python Dictionary duplicate keys
- Python dictionary increment value
- Check if two dictionaries are equal in Python
In this article, we have understood how to add elements to a set in Python. And also we have covered the following given topics.
- How to Add Elements in a Set in Python using add()
- How to Add Elements in a Set in Python using update()
- How to Add Elements in a Set in Python using union()
- How to Add Elements in a Set in Python using | operator
- How to Add Elements in a Set in Python using for loop and add function
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.