AppDividend
Latest Code Tutorials

Python Set: The Complete Guide

In this article, you will learn about Python sets, like how they are created, how you can add the items or remove the elements from them, and all operations performed on sets in Python. However, the Set itself is mutable. We can add or remove items from it. Set can be used to perform mathematical set operations like union, intersection, symmetric difference, etc.

Python Set

A set in Python is an unordered collection of elements or items. Every element in the Set is unique means no duplicates are allowed and must be immutable, which means it cannot be changed in the future.

Let’s take a simple example of Python Sets.

# app.py

setA = {'Samsung', 'Apple', 'Microsoft'}
print(setA)

See the output below.

 

Python Sets Tutorial With Example

How to create a Python Set

To create a set in Python, use the built-in function set(). The Set is formed by placing all the items inside the curly braces {}, and a comma separates the items. The Set can have any number of items, which may be of different types (integer, float, tuple, string, etc.). But the Set cannot have a mutable element, like a list, or dictionary, as its element.

Let’s create a complex datatypes Python Set.

# app.py

setA = {21, 'AppDividend', (21, 22, 19)}
print(setA)

See the output below.

Getting Started With Sets

How to access elements in Python Set

To access elements in Python set, loop through the Set. You cannot access items in the Set by referring to an index. Since sets are unordered, the items have no index. However, you can search for the specified value in a set by using them in the keyword.

# app.py

setA = {21, 'AppDividend', (21, 22, 19)}

for item in setA:
    print(item)

See the output below.

Access Items in Python Sets

How to add elements in Python Set

The Set is mutable. But since they are unordered, indexing has no meaning. To add one item to a set, we can use the add() method. If we wish to add more than one item to a set using the update() method.

See the below example.

# app.py

setA = {21, 'AppDividend', (21, 22, 19)}

setA.add(True)

print(setA)

In the above example, we have added one item, which is the type of Boolean. We can use the update() method to add multiple items to the Set.

# app.py

setA = {'Bad Bunny', 'Drake'}

setA.update({'Daddy Yankee', 'Louis Fonsi'})

print(setA)

Now, see the output below.

Add Items In Python Set

Get the Length of a Set

We can use the len function to determine the length of the Set.

# app.py

setA = {'Bad Bunny', 'Drake'}

setA.update({'Daddy Yankee', 'Louis Fonsi'})

print(len(setA))

See the output below.

Get the Length of a Set

How to remove element from Set in Python

We cannot access or change an item of the Set using indexing or slicing. ThisSet because the Set does not support it.

We can remove thSettem from the Set by specifying the item value.

# app.py

setA = {'Bad Bunny', 'Louis Fonsi', 'Daddy Yankee'}

setA.remove('Louis Fonsi')

print(setA)

So, the output value is the following.

Remove Item From Set

If an item to remove does not exist, the remove() method will raise an error.

Python Set Operations

Sets can be used to carry out the mathematical set operations like union, intersection, difference, and symmetric difference.

We can do this in both ways, which are operators or methods. Let’s see the following example.

Python Set Union

See the following example of Union Operation on Set.

# app.py

setA = {'Bad Bunny', 'Louis Fonsi', 'Daddy Yankee'}
setB = {'Justin Bieber', 'Drake'}

setUnion = setA|setB
print(setUnion)

See the below output.

Python Set Union Operation

In the above code line 3, we have performed the union operation between setA and setB. 

The union operation is performed using | operator. The same can be accomplished using the method union().

# app.py

setA = {'Bad Bunny', 'Louis Fonsi', 'Daddy Yankee'}
setB = {'Justin Bieber', 'Drake'}

setUnion = setA.union(setB)
print(setUnion)

It will give us the same output.

Python Set Intersection

The intersection of setA and setB is a set of common elements in both sets.

An Intersection is performed using operator. The same can be accomplished using the method intersect().

# app.py

setA = {'Bad Bunny', 'Louis Fonsi', 'Daddy Yankee'}
setB = {'Justin Bieber', 'Drake', 'Louis Fonsi'}

setUnion = setA.intersection(setB)
print(setUnion)

In the above example, we are getting the common items from both sets, Louis Fonsi. 

See the output below.

Python Set Intersection Example

There are still so many functions out there about Python Sets. We will see them briefly one by one on this blog.

That’s it for the Python set.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.