AppDividend
Latest Code Tutorials

Python Sets Tutorial With Example | Getting Started With Sets

0 21

Get real time updates directly on you device, subscribe now.

Python Sets Tutorial With Example | Getting Started With Sets is today’s topic. 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. 

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

However, the set itself is mutable. We can add or remove items from it. 

Sets can be used to perform mathematical set operations like union, intersection, symmetric difference, etc.

Python Sets Tutorial With Example

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

The set is formed by placing all the items inside the curly braces {} and the items are separated by comma or by using the built-in function set().

The set can have any number of items, and they 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

Access Items in Python Sets

You cannot access items in the set by referring to an index since sets are unordered the items has no index. 

But you can loop through the set elements using the for loop, or ask if the specified value is present 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

Add Items In Python Set

Sets are mutable. But since they are unordered, indexing has no meaning.

If we need to add one item to a set, we can use the add() method. If we wish to add more than one item then 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.

For adding the multiple items to the set, we can use the update() method.

Related Posts
1 of 42
# 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

Remove Item From Set

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

We can remove the item from the set by specifying 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 Example

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. 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 Example

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

An Intersection is performed using operator. 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 which is Louis Fonsi. See the output below.

 

Python Set Intersection Example

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

Finally, Python Sets Tutorial With Example | Getting Started With Sets is over.

Leave A Reply

Your email address will not be published.

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