Introduction to Set add()
In this article, we will see discuss how to add element to set using the add()
method in python. Set in Python is a one dimensional data structure that is stores unique values with multiple datatypes. add()
method adds an element to the set. If the element exists, then it will not be added to it.
Syntax:
Set_data.add(element)
Parameter:
It will take only one parameter I.E element to be added.
Example 1: Add elements to set
Let's create an empty set and add 5 elements into it.
# Create a set
my_set=set()
# Display the set
print(my_set)
# add 5 elements into the set
my_set.add(2)
my_set.add(24)
my_set.add(26)
my_set.add(28)
my_set.add(20)
# Display the set
print(my_set)
Output:
set()
{2, 20, 24, 26, 28}
Explanation:
Firstly, we created an empty set named my_set, After that we are adding 5 integers one by one to it.
Finally, we are displaying the set.
Example 2: Remove duplicates and add elements using loop
A set, by definition, contains unique elements--just as a dict’s keys are guaranteed to be unique. Thus, if you ever have a list of values from which you want to remove all of the duplicates, you can just create a set. You can create the set as in the solution code
unique_numbers = set(numbers)
or you can do so by creating an empty set, and then adding new elements to it:
numbers = [1, 2, 3, 1, 2, 3, 4, 1]
unique_numbers = set()
for number in numbers:
unique_numbers.add(number)
print(unique_numbers)
This example uses set.add, which adds one new element to a set.
{1, 2, 3, 4}
Bonus Tip
In case you don't want to run a loop just to add elements to a set and remove duplicates, so you can also use below approach. Here we can use the *
(splat) operator:
numbers = [1, 2, 3, 1, 2, 3, 4, 1]
unique_numbers = {*numbers}
print(unique_numbers)
This tells Python that it should take the elements of numbers and feed them (in a sort of for loop) to the curly braces. And indeed, this works just fine.
{1, 2, 3, 4}
Is it better to use set without the *
, or {}
with the *?
That’s a judgment call. I’m partial to the curly braces and *
, but I also understand that *
can be confusing to many people and might make your code less readable/maintainable to newcomers.
Example 3: Add tuple into set
Let's create a set and add elements to the set from the tuple.
# Create a set
my_set={1,2,3,4,5}
# Display the set
print("Set: ",my_set)
# Create a tuple
my_tuple=("welcome","to","golinuxcloud")
# Display the tuple
print("Tuple :",my_tuple)
# add all elements from my_tuple into the my_set
my_set.add(my_tuple)
# Display the final set
print("Final Set: ",my_set)
Output:
Set: {1, 2, 3, 4, 5}
Tuple : ('welcome', 'to', 'golinuxcloud')
Final Set: {1, 2, 3, 4, 5, ('welcome', 'to', 'golinuxcloud')}
Explanation:
Firstly, we created a set - my_set with 5 integers. After that we created a tuple - my_tuple that hold three strings.
We are adding entire tuple to the set using add()
method.
Finally we are displaying the set.
Summary
In this guide, we seen how to add elements to the set using add(
) method. It can be possible to add a tuple with elements to the set using the add()
method.