In this article we will discuss ways to add single or multiple elements to a set in python.

Add a single element to the set

Set in python provides a member function to append a single element to the set i.e.

set.add(element)

It accepts an element as an argument and if that element is not already present in the set, then it adds that to the set. It returns nothing i.e. None. We can use this to add a single value to the set. Let’s understand this with an example,

Adding a value to the set using add() function,

# Initialize a set
sample_set = {11, 12, "Hello", 13, "Hi"}

# Add a single element in set
sample_set.add(15)

print(sample_set)

Output:
{11, 12, 13, 'Hi', 15, 'Hello'}

As 15 was not present in the set, so add() function appended that to the set.

Adding a duplicate element to the set

As set contains only unique elements, so if try to add an element that already exist in the set, then it will have no effect. For example,

sample_set = {11, 12, "Hello", 13, "Hi"}

# If element is already present the add() function will not do anything
sample_set.add("Hello")

print(sample_set)

Output:
{11, 12, 13, 'Hi', 'Hello'}

As 15 was already present the set, so add() function did nothing with the set.

Adding immutable objects to the set using add() function

As Sets can only contain immutable elements, therefore we can add int, strings, bytes, frozen sets, tuples, or any other object that is immutable. For example, we can add a tuple to the set using add function,

sample_set = {11, 12, "Hello", 13, "Hi"}

# Add a tuple in set
sample_set.add((10, 20))

print(sample_set)

Output:
{11, 12, 13, 'Hi', (10, 20), 'Hello'}

Tuple is an immutable object; therefore, it can be added to a set. Whereas list in python is a mutable object, so we cannot add a list to the python using add() function. If you try to pass a list object to the add() function, then it will give error. For example,

Adding a mutable object like list to the set will raise TypeError

sample_set = {11, 12, "Hello", 13, "Hi"}

# Pass a list to add() will raise Error
sample_set.add([21, 31])

Error:
TypeError: unhashable type: 'list'

So, we can use the set.add() function to add a single element to the set. What if we want to add multiple elements to the set in one line ?

Adding multiple elements to the set

Set in python provides another function update() to add the elements to the set i.e.

set.update(*args)

It expects a single or multiple iterable sequences as arguments and appends all the elements in these iterable sequences to the set. It returns nothing i.e. None. We are going to use this update() function to add multiple value to the set in a single line,

Adding multiple values to the set using update() function

Suppose we have a set,

sample_set = {11, 12, "Hello", 13, "Hi"}

Now we want to add 3 more numbers in the set i.e. 10, 20 and 30. Let’s see how to do that,
# Add multiple elements in set by passing them as a tuple
sample_set.update((10, 20, 30))

print(sample_set)

Output:
{10, 11, 12, 13, 'Hi', 20, 30, 'Hello'}

We encapsulated all three elements in a tuple and passed that tuple as an argument to the update() function. Which iterated over all the elements in tuple and added them to the set one by one. Instead of passing a tuple, we can pass a list too i.e.
sample_set = {11, 12, "Hello", 13, "Hi"}

# Add multiple elements in set by passing them in list
sample_set.update(['a', 22, 23])

print(sample_set)

Output:
{'a', 11, 12, 13, 'Hi', 22, 23, 'Hello'}

update() function will iterated over all the elements in passed list and add them to the set one by one.

Adding elements from multiple sequences to the set

In update() function we can pass multiple iterable sequences and it will add all the elements from all the sequences to the set. Let’s understand by an example,

Suppose we have some elements in different iterable sequences like,

list_1 = ['a', 22, 23]
list_2 = [33, 34, 35]
sampe_tuple = (31, 32)

Now we want to add all the elements in above two lists and a tuple to the set. We can do that using update() function,
sample_set = {11, 12, "Hello", 13, "Hi"}

# Add multiple elements from different sequences to the set
sample_set.update(list_1, sampe_tuple , list_2)

print(sample_set)

Output:
{32, 33, 34, 'a', 35, 11, 12, 13, 'Hi', 22, 23, 31, 'Hello'}

We passed both the lists and the tuple to the update() function. It iterated over all of them and added all elements in them to the set.

Let see some more example of update() function,

Adding dictionary keys to the set

student_dict = {'John': 11, 'Rtika': 12, 'Aadi': 15}

sample_set = set()

sample_set.update(student_dict)

print(sample_set)

Output:
{'Rtika', 'John', 'Aadi'}

If we pass a dictionary to the update() function of set, then it will try to convert dictionary object to an iterable sequence. Dictionary will be implicitly converted to sequence of keys and update() function will add all the keys in the dictionary to the set.

Adding dictionary values to the set

student_dict = {'John': 11, 'Rtika': 12, 'Aadi': 15}

sample_set = set()

sample_set.update(student_dict.values())

print(sample_set)

Output:
{11, 12, 15}

We passed a sequence of all values of the dictionary object to the update(). Which added them to the set. As set contains only unique elements, so now it contains only unique values of the dictionary. So, this is how we can add single or multiple elements in the set using add() or update() function.

The complete example is as follows,

def main():

    # Initialize a set
    sample_set = {11, 12, "Hello", 13, "Hi"}

    print('Original Set:')
    print(sample_set)

    print('**** Add a single element to the set ****')

    # Add a single element in set
    sample_set.add(15)

    print('Set contents: ')
    print(sample_set)

    print('** Adding a duplicate element to the set **')

    sample_set = {11, 12, "Hello", 13, "Hi"}

    # If element is already present the add() function will not do anything
    sample_set.add("Hello")

    print('Set contents: ')
    print(sample_set)

    print('** Adding an immutable objects to the set **')

    sample_set = {11, 12, "Hello", 13, "Hi"}

    # Add a tuple in set
    sample_set.add((10, 20))

    print('Set contents: ')
    print(sample_set)

    print('Adding a mutable object like list to the set will raise Error')

    # Can not pass a list object (mutable) to the add() function of set
    # sample_set.add([21, 31])

    print('*** Adding multiple elements to the set ***')

    sample_set = {11, 12, "Hello", 13, "Hi"}

    # Add multiple elements in set by passing them as a tuple
    sample_set.update((10, 20, 30))

    print('Set contents: ')
    print(sample_set)

    sample_set = {11, 12, "Hello", 13, "Hi"}

    # Add multiple elements in set by passing them in list
    sample_set.update(['a', 22, 23])

    print('Set contents: ')
    print(sample_set)

    print('*** Adding elements from multiple sequences to the set ***')

    sample_set = {11, 12, "Hello", 13, "Hi"}

    list_1 = ['a', 22, 23]
    list_2 = [33, 34, 35]
    sampe_tuple = (31, 32)

    # Add multiple elements from different sequences to the set
    sample_set.update(list_1, sampe_tuple , list_2)

    print('Set contents: ')
    print(sample_set)

    print('*** Adding dictionary keys to the set ***')
    student_dict = {'John': 11, 'Rtika': 12, 'Aadi': 15}

    sample_set = set()

    sample_set.update(student_dict)

    print(sample_set)

    print('*** Adding dictionary values to the set ***')

    sample_set = set()
    sample_set.update(student_dict.values())

    print(sample_set)


if __name__ == '__main__':
   main()

Output
Original Set:
{'Hi', 11, 12, 13, 'Hello'}
**** Add a single element to the set ****
Set contents: 
{'Hi', 11, 12, 13, 15, 'Hello'}
** Adding a duplicate element to the set **
Set contents: 
{'Hi', 11, 12, 13, 'Hello'}
** Adding an immutable objects to the set **
Set contents: 
{'Hi', 11, 12, 13, 'Hello', (10, 20)}
Adding a mutable object like list to the set will raise Error
*** Adding multiple elements to the set ***
Set contents: 
{10, 'Hi', 11, 12, 13, 'Hello', 20, 30}
Set contents: 
{'a', 'Hi', 11, 12, 13, 'Hello', 22, 23}
*** Adding elements from multiple sequences to the set ***
Set contents: 
{32, 33, 34, 35, 'a', 'Hi', 11, 12, 13, 'Hello', 22, 23, 31}
*** Adding dictionary keys to the set ***
{'Aadi', 'John', 'Rtika'}
*** Adding dictionary values to the set ***
{11, 12, 15}