How To Add or Append Values To a Set In Python

Do you want to add or append values to a Set in Python? This article will show you how to add values to a set in Python.

A set is a type of dictionary that does not allow duplicate entries. As a result, if you want a list of unique values, you should use a set.

To add a single value to a set in Python, use the add() method, which is available for Set. If you want to add multiple values to a set at once, you must use the update() method.

How To Add Values To A Empty or Non-Empty Set Using Add()

You can add any values or elements to the set using the add() method. This set can be empty or nonempty; either way, the values will be added. However, in the case of the Add method, you can only add a single element at a time.

Let’s look at how the Add Method is used in the code below.

#Creating a Empty Set
givenSet = set()

#Adding Single Item to Set
givenSet.add('A')
givenSet.add('B')

#Printing the Set
print(givenSet)

Output:

{'A', 'B'}

As you can see, we added two elements one by one using the set class’s add method.

How to Append Multiple Values to a Set in Python

In Python, you must use the update() method to append multiple values to a Set. Alternatively, you can use ‘|’ to concatenate the list of values to the set.

Let’s look at how to update and ‘|’ are used in the code below.

Using Update Method

#Creating a Empty Set
givenSet = set()

#Adding Single Item to Set
givenSet.add('A')
givenSet.add('B')

listToAddToSet = ['C', 'D']

givenSet.update(listToAddToSet)

#Printing the Set
print(givenSet)

Output:

{'D', 'B', 'A', 'C'}

As you can see from the code above, we can easily add multiple values to a set by using the update method.

Using ‘|’ to Append Multiple Values to a Set

#Creating a Empty Set
givenSet = set()

#Adding Single Item to Set
givenSet.add('A')
givenSet.add('B')

givenSet = givenSet | {'C', 'D'}

#Printing the Set
print(givenSet)

Output:

{'C', 'A', 'D', 'B'}

So, in Python, we were able to add another set of values to the given set by using ‘|’.

Using Union Method To Add Multiple Values to A Set

There is a Union[1] method present in the set class that can be used to add or append different set values into a single set.

Let’s look at how to use the union method in Python in the code below.

#Creating a Empty Set
givenSet = set()

#Adding Single Item to Set
givenSet.add('A')
givenSet.add('B')

#Performing Union of Two Set
givenSet = givenSet.union({'C','D'})

#Performing Union with list of Values
givenSet = givenSet.union(['E', 'F'])

#Printing the Set
print(givenSet)

Output:

{'F', 'E', 'D', 'C', 'A', 'B'}

Using the above code, we were able to successfully add a list of values as well as the different set values to the existing set of values.

How To Add or Append Values To a Set In Python

Wrap Up

I hope you were able to find an answer to your question about how to add or append values to a set in Python. You learned four different ways to do so in this lesson.

The first method describes how to add a single value to a Python set. In Python, every other method refers to the addition of multiple values to a set.

If you have a better method than the one discussed above, please let me know and I will gladly add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. 4 Ways To List All Subdirectories in a Directory – Python
  2. Dict To list – Convert A Dictionary to a List in Python
  3. Discord.py Bot Add Reaction To a Message Python
  4. Python Get The Minimum Value In Dictionary with Key
  5. How To Append Multiple Values To A List in Python
  6. How To Reboot or Restart Python Script

Leave a Comment