Python Set Add Method with Examples

In this session, we will learn about the Python Set add() method with the help of examples.

This post will demonstrate how to add a single or more entries to a Python Set using examples. To add elements to Set, we will utilize the Python Set built-in functions add() method.

But before that, let’s define what is set in Python, so…

Sets in Python are used to hold unique elements or objects. Sets, unlike other data structures such as tuples and lists, do not permit the addition of duplicate values.

Moving on…

We will examine various Python methods for adding an element to a set. We will also examine various scenarios in which it is prohibited to add certain object types to a set.

Also read: Python Nonlocal Methods Made Simple with Examples

What is Set Add() Method in Python?

Python’s set add() function is a built-in method; if an element is missing from the set, it adds the element to the set. This method only works if the element is not already present in the set.

Example of how it works:

Myset = {"ben", "may", "boy"}

Myset.add("rose")

print(thisset)

Output:

{'may', 'rose', 'ben', 'boy'}

In fact, you can use the Python add to a set if you wish to add a new item to the set. The method is located in the Python standard library.

If the element is already part of that set, then it does not add that element since, as we all know, the set does not have any duplicate values within it.

The Add() to set python is used to add new values to a set. When invoked on a set, the add() method takes a single element to be added to the set as an input parameter and adds it to the set.

When the input element is already present in the set, nothing happens. After successful execution, The add() method returns None.

On the other hand, the update() method is used to add multiple elements to a set. The update() method takes one or more iterable objects such as a python dictionary, tuple, list, or set, and adds the elements of the iterable to the existing set.

Syntax of Python Set Add

set.add(elem)

The add() method doesn’t add an element to the set if it’s already present in it otherwise it will get added to the set.

Set Add Python Parameters

add() method takes a single parameter:

  • elem – the element that is added to the set

Here, an element is a value that is to be added to the set.

Return Value From python add to set

add() method doesn’t return any value and returns None.

It means that set() method does not return any value or anything. It just adds the given value if it is not present in that set.

Example Program of Python Set Add Method

Here are different example programs that utilized Python Set Add Method

Example 1: Add an element to a set

# set of consonants
consonants = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k'}

# adding 'l'
consonants.add('l')

print('Consonants are:', consonants)

# adding 'l' again
consonants.add('l')

print('Consonants are:', consonants)

The example program above has a set of ‘consonants’, using the set add methods we are going to add an element “l” in the consonant set. Then, to see if the element is added we use print() method.

After which, we add again the element ‘l’ to consonant set and use print() method to see the output, as you can see it will not be added in the second output if we run the code since the set already contains the element ‘l’.

Output:

Consonants are: {'j', 'l', 'g', 'k', 'd', 'f', 'c', 'h', 'b'}
Consonants are: {'j', 'l', 'g', 'k', 'd', 'f', 'c', 'h', 'b'}

Example 2 of adding element in a set:

# set of numbers
number = {'1', '2', '3'}

# adding '4'
number.add('4')
print('Numbers are:', number)

# adding '5' 
number.add('5')
print('Numbers are:', number)

The example program above has a set of ‘numbers’, using the set add methods we are going to add an element 4 in the number set. Then, to see if the element is added we use print() method.

After which, we add again another element ‘4’ to number set and use print() method to see the output, as you can see it will appear in the second output if we run the code since the element added is not present on the number set.

Output:

Numbers are: {'4', '3', '1', '2'}
Numbers are: {'4', '2', '3', '1', '5'}

Add tuple to a set

Tuples are used in programming to store numerous things in a single variable. A tuple is one of four built-in data types in Python that are used to store data collections. The other three are List, Set, and Dictionary, all of which have different properties and applications.

To add a tuple to a set, use the set() method. Add the following code.

Example 1: Add a tuple to a set

# set of colors
color = {'red', 'blue', 'green'}

# a tuple ('yellow', 'orange')
tup = ('yellow', 'orange')

# adding tuple
color.add(tup)

print('Colors are:', color)

# adding same tuple again
color.add(tup)

print('Colors are:', color)

The example program shows how to add a tuple to a set of ‘colors’ by utilizing the set add methods to add a tuple element to the color set. The print() method is then used to check if the element has been added.

Then we add another of the same tuple to the color set and use the print() method to see the output, which is the same as the first tuple added because the tuple element is the same.

The output of the program

Colors are: {('yellow', 'orange'), 'red', 'green', 'blue'}
Colors are: {('yellow', 'orange'), 'red', 'green', 'blue'}

Example 2: Add tuple to a set

# app.py

# Writing Pythonforfree char by char in a set
website = {'P', 'y', 't', 'n', 'f', 'o', 'r', 'f','r','e','e'}

print("Before adding set is: ", website)

# Here we have not included one 'h' and 'o'
# Here, we will add 'h' and 'o' in a tuple
# Then we will add it to the set

tup = ('h', 'o')
# adding
website.add(tup)

print("After adding tuple, set is: ", website)

The example code demonstrates adding a tuple to a set. First, we write Pythonforfree char by char in a set.

Then, print the elements in the set, here we have not included one ‘h’ and ‘o’.

After which, we will add ‘h’ and ‘o’ in a tuple, and then we will add them to the set.

Output:

Before adding set is:  {'o', 'y', 't', 'n', 'P', 'f', 'r', 'e'}
After adding tuple, set is:  {'o', 'y', ('h', 'o'), 't', 'n', 'P', 'f', 'r', 'e'}

Conclusion

In this tutorial, we learned how to use add() method to add elements to a set with the help of well-detailed Python programs.

Leave a Comment