Methods and Functions

Python Set add()

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.

python add to set

Set add() Parameters

We use the following syntax to use the add() method:

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:

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:

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.