How to Add to a Set in Python

by | Programming, Python, Tips

This tutorial will go through adding elements to a Python set using the add() method.


Adding to a Set using add()

Syntax of Python Set add()

The syntax of the add() method is

set.add(element)

The add() method adds elements to the set in place. The method does not return a new set.

Example#1: Add Number to a Set

We can use the add() method to add a given element to a set. Sets do not store duplicate values, so if we try to add an element already present in the set, add() will not add any element.

Let’s look at an example of a set of even numbers.

We will add another even number to the set:

even_numbers = {2, 4, 6, 8}

# Add 10 to even_numbers

even_numbers.add(10)

print(even_numbers)

Let’s run the code to see the result:

{2, 4, 6, 8, 10}

If we try to add an element that already exists in the set, the add() method will not add the element. For example:

even_numbers = {2, 4, 6, 8}

# Attempt to Add 2 to even_numbers

even_numbers.add(2)

print(even_numbers)
{8, 2, 4, 6}

Note that the Python set does not maintain data order, so if you add or remove elements from a set, the order may change.

Example 2: Add Tuple to a Set

We can add tuples to a set. Let’s look at an example of adding a tuple of strings to a set of strings:

# set of strings

characters = {"Han", "Wookie", "Jabba"}

# Tuple

tup = ("Luke", "Leia")

# Add tuple to set

characters.add(tup)

print(f'The set of characters are: {characters}')

# Trying to add the tuple again

characters.add(tup)

print(f'The set of characters are: {characters}')

Let’s run the code to see the result:

The set of characters are: {'Jabba', 'Wookie', 'Han', ('Luke', 'Leia')}
The set of characters are: {'Jabba', 'Wookie', 'Han', ('Luke', 'Leia')}

Like singular elements, you can only add the same tuple once to a set.

What are the Uses of a Python Set?

A Python set is one of the four built-in data types in Python to store collections of data. A set is a collection that has no duplicate elements, is unordered, unchangeable and not indexed. We can use sets for membership testing in a collection of items and removing duplicates. For further reading on the use of sets for removing duplicates, go to the article How to Get Unique Values from List in Python. Set objects also support mathematical operations like union, intersection, difference and symmetric difference. Union and intersection are the components of Jaccard similarity, which is a ubiquitous similarity measure in statistics.

Summary

Congratulations on reading to the end of this tutorial. We have gone through how to add elements to a set. You can add elements to a set using the add() method. If the element already exists in a set, using add() for that element will not add it to the set.

For further reading on set methods, go to the article: How to Remove an Element From a Set in Python.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee