Page Contents
In this tutorial we will learn how to add an element to a set in Python . We will do it using 4 ways . They are as follows –
- Python Set
add()
method - Python Set
update()
method - Python Set update(
|=
) operator . - Python Set union(
|
) operator
Let us have a look about all this ways with examples –
Python Set add() method
It adds an element to the given set . The element must be of a hashable type .
Syntax of add() method
set.add(element)
Parameters of add() method
- element = It is a hashable type python object which will be added to the given set .
Return type of add() method
It returns None .
Examples of Python Set add() method
Example 1: Add an integer to a set
# declaring a set
myset={1,3,4,8 ,9}
# add 7 to the set
myset.add(7)
# printing the set
print(myset)
{1, 3, 4, 7, 8, 9}
- In the above code first we have declared a set named
myset
as{1,3,4,8 ,9}
. - Then we have added an integer
7
with the set usingadd()
method . - From the output we can see that
7
is added to the set . As set is unordered so7
can be added to any place in the set .
Example 2: Add a tuple to a set
# declaring a set
myset={1,3,4,8 ,9}
# add a tuple to the set
myset.add((8,9))
# printing the set
print(myset)
{1, 3, 4, (8, 9), 8, 9}
In the above code we have added a tuple to a set .
Note :
As list , set , dictionary are not a hashable type Python object so if we try to add list , set , dictionary to a set then it will raise TypeError
.
Example : Try to add unhashable object to a set
# declaring a set
myset={1,3,4,8 ,9}
# add a list to the set
myset.add([8,9]) # error
# add a dictionary to the set
myset.add({8:1,9:1}) # error
# add a set to the set
myset.add({8,9}) #error
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_8720/3787350739.py in <module> 3 4 # add a list to the set ----> 5 myset.add([8,9]) # error 6 7 # add a dictionary to the set TypeError: unhashable type: 'list'
Python Set update() method
It adds multiple elements from one or more iterables to the given set . The elements of the iterables must be of a hashable type .
Syntax of update() method
set.update(iterables)
Parameters of update() method
- iterables = One or more Python iterables that should contain only hashable type element .
Return type of update() method
It returns None .
Examples of Python Set update() method
Example 1: Add elements from an iterable to a set
# declaring a set
myset={1,2,3,4}
# add elements from an iterables to the set
myset.update((8,9))
# printing the set
print(myset)
{1, 2, 3, 4, 8, 9}
- In the above code first we have declared a set named
myset
as{1,2,3,4}
. - Then we have added elements from an iterable
(8,9)
to the set usingupdate()
method . - From the output we can see that each element from the iterable is added to the set .
Example 2: Add elements from multiple iterables to a set
# declaring a set
myset={1,2,3,4}
# add elements from multiple iterables to the set
myset.update((8,9),{5,6},[11,12])
# printing the set
print(myset)
{1, 2, 3, 4, 5, 6, 8, 9, 11, 12}
In the above code we have added multiple elements from various type iterables to the given set .
Python Set update(|=
) operator
It adds elements from a set to the given set .
Syntax of update(|=
) operator
set|=other
Parameters of update(|=
) operator
- other = It is a set or an expression that can be evaluate to a set .
Return type of update(|=
) operator
It returns None .
Examples of Python Set update(|=
) operator
Example 1: Merge two sets in Python
# declaring a set
myset={1,2,3,4}
# adding elements
myset|= {5,6,7}
# printion the set
print(myset)
{1, 2, 3, 4, 5, 6, 7}
In the above code we have added a set {1,2,3,4}
with an other set {5,6,7}
using update(|=
) operator .
Example 2:
# declaring a set
myset={1,2,3,4}
# adding elements
myset|= {item for item in range(5,8)}
# printing the set
print(myset)
{1, 2, 3, 4, 5, 6, 7}
In the above code we have added a set {1,2,3,4}
with a set comprehension using update(|=
) operator .
Python Set union (|) operator
It returns a new set after adding elements from two set .
Syntax of union(|
) operator
set|other
Parameters of union(|
) operator
- other = It is a set or an expression that can be evaluate to a set .
Return type of union(|
) operator
It returns a set .
Examples of Python Set union(|
) operator
Example 1: Merge two sets in Python
# declaring two set
myset1={1,2,3,4}
myset2={5,6,7}
# adding elements of two sets
myset = myset1 | myset2
# printing the set
print(myset)
{1, 2, 3, 4, 5, 6, 7}
In the above code we have added elements from two sets using union (|
) operator .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .