How To Add Values To Python Set

In this article, you will learn how to add values to Python Set. As you might already know that a set a list of values that do not accept duplicate values.

Meaning if you have an element already present in the set then if you try adding it in the set then there will be only one element and it will not create two duplicate elements as it happens in the normal list of Python.

Using add Method to Add Values to Python Set

In the below code we will see how you can add or insert a normal element to the set. And then in the later example code, you will see the addition of a duplicate item that is not reflected in the output.

#Initializing Set of Alphabets
setOfAlpha = {'a', 'b', 'c'}

#adding Elements to Set in Python
setOfAlpha.add('d')

#Printing the set to check if d is added
print("Elements of Set are: {}".format(setOfAlpha))

Output:

Elements of Set are: {'a', 'd', 'b', 'c'}

Adding Duplicate Element in Set

In the below example, we will try adding duplicate elements in the above set element for example ‘a’. Since ‘a’ is already present in the set hence after adding ‘a’ again and printing the set should print only a single ‘a’ element in the set.

#Initializing Set of Alphabets
setOfAlpha = {'a', 'b', 'c', 'd'}

#adding Duplicate Elements to Set in Python
setOfAlpha.add('a')

#Printing the set to check if d is added
print("Elements of Set are: {}".format(setOfAlpha))

Output:

Elements of Set are: {'a', 'b', 'c', 'd'}

Set vs List in Python

As you have already seen in the above code that set does not allow you to add similar or duplicate elements in your set. Hence to add the duplicate element you need to use a list.

In python, the list is similar to arrays where you can add any type of element and delete them accordingly. In the below code we will see how Set vs List in Python actually works.

#Initializing Set of Alphabets
setOfAlpha = {'a', 'b', 'c', 'd'}

#adding Duplicate Elements to Set in Python
setOfAlpha.add('a')

#adding Duplicate Element in List of Python
listOfAlpha.append('a')

#Printing the set to check if a is added
print("Elements of Set are: {}".format(setOfAlpha))
print("Elements of List are: {}".format(listOfAlpha))

Output:

Elements of Set are: {'b', 'c', 'd', 'a'}
Element of List are: ['a', 'b', 'c', 'd', 'a']
How To Add Values To Python Set

As you can see in the above output for the list you are getting output that is having ‘a’ listed two times but that is not the case with the set.

That is all for this tutorial on how to add values to a Python set. If you have any queries or suggestions please post that in the comment section so that we can answer your queries asap.

Please follow us on Facebook and Twitter. We will be soon coming with Free Courses on Python on all the topics such as Beginner, Intermediate, and Advance. Keep following us for further updates. Also, signup for the below newsletter to keep posted once we launch the courses.

Further Read:

  1. JSONPath in Python How to Parse With Examples
  2. Python Check If File is Empty – 4 Ways

Leave a Comment