Python Sets : A Comprehensive Guide

Python is a really flexible and easy-to-learn language that’s become super popular because it’s simple, easy to understand, and can do a whole bunch of different things.

Python offers a rich set of data structures to facilitate efficient manipulation and storage of data. Sets are one of its built-in data structures. In this blog, you will learn about Python sets and their methods.

Let’s jump right into it!πŸš€

What are Sets?πŸ€”

A set is an unordered collection of data items and is used to store multiple items in a single variable.

In a set, items are separated by commas and enclosed within curly brackets {}.

Sets are unordered which means the sequence in which the items will appear cannot be guaranteed.

Sets are immutable which means you cannot change items of the set once created, but you can remove items and add new items.

Sets do not contain duplicate items.

Example:

Output:

Here you can see that the items of the set occur in random order, so they cannot be accessed using index numbers.

Also in the set, the value 36 was two times but in the output it appeared only one time which means sets do not allow duplicate values.

How to Create an Empty Set?πŸ€”

Let’s try to create an empty set by leaving the curly brackets empty.

Now, let’s see the output of the above code.

🫨 It’s a dictionary, not a set. Then how do I create an empty set?😟

Okay, so for creating an empty set simply just use set() as follows:

Let’s see the output of the above code.

So for creating an empty set instead of leaving the curly brackets empty, use set().

How to Access Set Items?πŸ€”

Since set items do not have index numbers, so you can access items of set using a loop.

For example:

Output:

How to check if an item exists in the Set?πŸ€”

You can check the presence of an item in a set using the in keyword.

For example:

Output:

Python Set union()

The union() method is used to get all the items that are present in two sets. This method returns a new set.

For example:

Output:

Python Set intersection()

The intersection() method is used to get only items that are common in both sets. This method returns a new set.

For example:

Output:

Python Set intersection_update()

The intersection_update() method is similar to the intersection() method but the difference is that intersection_update() method updates into the existing set from another set.

For example:

Output:

Python Set symmetric_difference()

The symmetric_difference() method is used to get the items that are not common to both sets. This method returns a new set.

For example:

Output:

Python Set symmetric_difference_update()

The symmetric_difference_update() method is similar to the symmetric_difference() method but the difference is that symmetric_difference_update() method updates into the existing set from another set.

For example:

Output:

Python Set difference()

The difference() method is used to get items that are only present in the original set and not in both sets. This method returns a new set.

For example:

Output:

Python Set difference_update()

The difference_update() method is similar to the difference() method but the difference is that difference_update() method updates into the existing set from another set.

For example:

Output:

How to manipulate set items?πŸ€”

You can manipulate set items by using the following built-in methods.

add()

If you want to add a single item to the set, then you can use the add() method.

For example:

update()

If you want to add more than one item to the set, then you can create another set or any other iterable object(list, tuple, dictionary), and use the update() method to add it to the existing set.

For example:

remove()/discard()

If you want to remove items from the set, then you can use the remove() and discard() methods.

For example:

The main difference between these two methods is that, if you try to delete an item that is not present in the set, then remove() method raises an error, whereas discard() method does not raise any error.

Example1:

Example2:

del

If you want to delete a set entirely, then you can use del.

del is a keyword, not a method.

For example:

Here you get an error because the entire set has been deleted and there is no variable called set1 anymore.

But what if I want to delete all items of the set, not the entire set? πŸ€”

Then you have the clear() method.

clear()

If you want to clear all the items of a set, then you can use the clear() method. This gives an empty set.

For example:

pop()

If you want to remove the last item of a set, then you can use the pop() method.

Note: In this method, you can’t determine which item gets popped as the sets are unordered.

For example:

But I want to know which item has been popped out.☹️

Okay, to get the popped item just simply assign it to a variable.

For example:

isdisjoint()

If you want to check whether the items of a given set are present in another set or not, then you can use the isdisjoint() method.

This method returns False if items are present, else it returns True.

For example:

Here, 13 of set2 is present in set1, so it returns False.

issuperset()

If you want to check if all the items of a particular set are present in the original set, then you can use the issuperset() method.

This method returns True if all the items are present, else it returns False.

Example:1

Here, all the items of set2 are not present in set1, so it returns False.

Example:2

Here, all the items of set2 are present in set1, so it returns True.

issubset()

If you want to check if all the items of the original set are present in the particular set, then you can use the issubset() method.

This method returns True if all the items are present, else it returns False.

Example:1

Here, all the items of set1 are not present in set2, so it returns False.

Example:2

Here, all the items of set1 are present in set2, so it returns True.

That’s all for today.

I hope it was helpful.

Thanks for reading.

For more content like this click here.

You can also follow me on X(Twitter) for getting daily tips on web development.

Keep Coding!!

Leave a Comment