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.

Set is a type of dictionary where you cannot have any duplicate entries. Hence if you want a list of unique values then you should use a set.

To add a single value to set in python you need to use add() method present for Set in Python. And if you want to add multiple values at once in a set, you need to use the update() method.

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

You can use the add() method to add any values or elements to the set. This set can be either empty or nonempty it will add the values. But in the case of Add method, you can only add a single element at once.

Let us see in the below example code the usage of Add Method.

#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 have added two elements one by one using the add method present in the set class.

How to Append Multiple Values to a Set in Python

If you want to append multiple values to a Set in Python, you need to use the update() method. Or you can also use ‘|’ to concatenate the list of values to the set. Let us see in the below example code the usage of update and ‘|’.

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 saw in the above code, we could easily add multiple values to a set 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'}

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

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 us see in the below example code the usage of the union method in Python.

#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 add a list of values as well as the different set values to the existing set of the values successfully.

How To Add or Append Values To a Set In Python

Wrap Up

Hope You were able to get the answer related to how to add or append values to a set in Python. In this, you learned four different methods to do so.

The first method tells you how to add a single value to a set in Python. And all other method talks about the addition of multiple values to a set in Python.

Let me know if you have any better method than the one discussed above I will be happy to 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
  7. Python: To Check If String Contains a Substring

Leave a Reply

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