Python Set Add Method with Examples

What is Set Add() Method in Python?

Sets in Python are used to hold unique elements or objects.

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.

Unlike other data structures such as tuples and lists, do not permit the addition of duplicate values.

This method only works if the element is not already present in the set.

To add elements to Set, we will utilize the Python Set built-in functions add() method.

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 the set() method does not return any value or anything. It just adds the given value if it is not present in that set.

Example of how it works:

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

Myset.add("rose")

print(thisset)

Output:

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

To add a new item to a Python set, use the add() method from the standard library. It ensures no duplicate values are added.

If the element is already in the set, nothing happens, and the add() method returns None.

For adding multiple elements, you can use the update() method. It takes iterable objects like dictionaries, tuples, lists, or sets and adds their elements to the existing set.

Example Program of Python Set Add Method

Here are different example programs that utilized the 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 the print() method.

After which, we add again the element ‘l’ to consonant set and use the 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 a 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.

You may also check Python Nonlocal Methods Made Simple with Examples.

Leave a Comment