Sets are unorganized and unindexed collections of objects that do not allow duplicates to exist. The Python add to set method lets you add new items to a set. However, if the element already exists, it does not add anything.
Set add() Parameters
We use the following syntax to use the add() method:
set.add(element)
The method accepts only one parameter: the element you want to add.
Note that if you use add() while creating an object, the method does not give you back a set.
noneValue = set().add(elem)
The above statement returns the return type of add: None.
Return Value from Set add()
The add() method does not return any value and only returns None.
Example of how to add an element to a set
Let us see how we can use Python add() to set through an example code.
# letters set
lett = {'a', 'l', 'l', 'i', 's', 'g'}
# adding 't'
lett.add('t')
print ('The letters are:', lett)
# adding 'l' again
lett.add('l')
print ('The letters are:', lett)
Output:
The letters are: {'a', 'l', 'g', 't', 's', 'i'}
The letters are: {'a', 'l', 'g', 't', 's', 'i'}
Observe that the second ‘b’ does not get added to the set. The letters print in a different order since sets are unordered sequences.
Example of adding tuple to a set
Similar to adding individual elements, you can also add tuples to a set.
# letters set
lett = {'a', 'l', 'l', 'i', 's', 'g'}
# creating a tuple
tup = ('o', 't', 'q')
# adding the tuple
lett.add(tup)
print ('The letters are:', lett)
# adding the same tuple again
lett.add(tup)
print ('The letters are:', lett)
Output:
The letters are: {'l', ('o', 't', 'q'), 'a', 's', 'g', 'i'}
The letters are: {'l', ('o', 't', 'q'), 'a', 's', 'g', 'i'}
Questions and Answers
Q1. How do you add to a set in Python?
You can add elements and tuples to a set by using the add() method. Since sets do not allow duplications, you can add the items only once. If you try to add the elements again, the function does not add anything to the set.
Q2. Can we add a list to set in Python?
No, you cannot actually add a list to a set since lists are mutable and unhashable objects, and sets are immutable and do not accept unhashable objects.
However, You can add individual elements of a list to a set by using the update() method.
# letters set
lett = {'a', 'l', 'l', 'i', 's', 'g'}
# creating a list
lis = ['o', 't', 'q']
# updating the set with list
lett.update(lis)
print ('New letters:', lett)
Output:
New letters: {'s', 'i', 'g', 't', 'q', 'o', 'l', 'a'}
Q3. How do you add an element to a set?
To add an element to a set, we follow the syntax:
set.add(element)
You pass the elements you want to add as function arguments.
# numbers set
num = {45, 34, 7, 36, 23}
num.add(8)
print ('The numbers are:', num)
Output:
The numbers are: {34, 36, 7, 8, 45, 23}
Q4. How do you add to an empty set in Python?
You can add elements to an empty set by using the add() method.
# creating an empty set
num = set()
# adding elements
num.add(4)
num.add(9)
print ('Numbers:', num)
Output:
Numbers: {9, 4}
Leave a Reply