Python: How to add or append values to a set ?

How to add or append values to a set in Python ?

In python add() method or append() method is used to simply add an item in set, tuple, dictionary etc.. but this method doesn’t add an element  if the entered element already present before it.

Adding a single element to the set :

Set is a data type which stores multiple items in a single variable and the items can be of different types.

Syntax : set.add(element)

It accepts an element as an argument and if that element is not present then it add it o the set.

Add a value to the set using add() function :

#program :

#Set initialized
my_set = {1, 2, "Hello", 3, "Hi"} 
# Adding a single element in set 
my_set.add(5) 
print(my_set)
Output :
{1,2,3,5'Hello','Hi'}

Adding a duplicate element to the set :

As set contains only unique elements, so duplicate elements so it will not not add the element again means set has no effect.

Let’s see an example program for it.

#program :

#Set initialized
my_set = {1, 2, "Hello", 3, "Hi"} 
# Adding 'Hello' in set 
my_set.add("Hello") 
print(my_set)
Output :
{1,2,3,5'Hello','Hi'}

Adding immutable objects to the set using add() function :

Set contains immutable elements. So, int, strings, bytes, frozen sets, tuples can be added as these are immutable object. But we can not add a list as it is mutable object.

Let’s see an example program for it how we can add a tuple to the set using add function.

#Program :

#Set initialized
my_set = {1, 2, "Hello", 3, "Hi"}
# Adding a tuple in set
my_set.add((4, 5))
print(my_set)
Output :
{1, 2, (4,5),"Hello", 3, "Hi"}

Adding a mutable object to set will give TypeError :

Suppose we are trying to add a list as it is mutable object. It will give Type error.

Let’s see an example program for it.

#Program :

#Set initialized
my_set = {1, 2, "Hello", 3, "Hi"}
# Adding a tuple in set
my_set.add([21, 31])
print(my_set)
Output :
TypeError: unhashable type: 'list'

Adding multiple elements to the set :

By using update() method we can add elements to the set.

Syntax : set.update(*args)
#Program :

#Set initialized
my_set = {1, 2, "Hello", 3, "Hi"}
# Adding 10,20,30 in set
#update() method is used
my_set.update((10, 20, 30))
print(my_set)
Output :
{1,2,3,5'Hello','Hi',10,20,30}

Adding elements from multiple sequences to the set :

Suppose we want to add a list, tuple at a time. Then let’s see how it works.

#Program :

#Set initialized
my_set = {1, 2, "Hello", 3, "Hi"}

#list initialized
my_list= [10, 20, 3]
#tuple initialized
my_tuple = (31, 32)
#update() method is used
my_set.update(my_list,my_tuple)
print(my_set)
Output :
{1,2,3,5'Hello','Hi',10,20,30,31,32}

Adding dictionary keys to the set :

#Program :

#Set initialized
my_set = set()
#dictionary initialized
my_dict = {'Satya': 11, 'Omm': 12, 'Nitya': 15}
#only keys ill be added
my_set.update(my_dict)
print(my_set)
Output :
{'Satya','Omm','Nitya'}

When we will pass the dictionary inside the update() method then only key will be added in the set.

Adding dictionary values to the set :

#Program :

#Set initialized
my_set = set()
#dictionary initialized
my_dict = {'Satya': 10, 'Omm': 20, 'Nitya': 30}
#only keys ill be added
my_set.update(my_dict.values())
print(my_set)
Output :
{10,20,30}

When we will pass the dictionary_name.values() inside the update() method then only values will be added in the set.