Python Set With Examples

In this tutorial we are going to learn Python Set. In our previous article we learnt about Python String. You can learn it from here.

 Python Set.

Python Set

Python Set is an unordered collection of unique elements. Suppose you have a list and you need only the unique items of the list you can use Python Set. Similarly, if you need only unique items from input, Python set can help you to do so. You can add or delete items from it.

You can initialize a set by placing elements in between curly braces. Like other sequences, one set can have elements of multiple data-types. Moreover, you can also create a set from a list by using set() function. The following example will give you some idea about initializing a set.

The output will be

Adding Elements to Python Set

In previous example, we learned how to initialize Python set directly. Suppose we need to add element to set, we can do so by using add() function. But this function can add a single element. If you want to add iterable elements like list or set, you can do so by using update() function. The following example will help you understand the thing

The output of the following code will be

Remove Elements from Python Set

There are two functions to remove elements from Python Set. One is remove() and another is discard() function. If the element you are trying to remove is not in the set, the remove() function will raise exception for this. But the discard function will not do anything like this. The following code will show you those

You will find the output be like,

Python Set Operations

You might be familiar with some mathematical set operations like union, intersection, difference. We can also do those using Python set. Now, we will learn how to do that.

Python Set Union

Union is the operation to merge two sets. That means, union will create another set that contains all unique elements of two sets. For example, {1, 2, 3, 4} and {2, 3, 5, 7} are two sets. If we do union operation over them, we get {1, 2, 3, 4, 5, 7}. We can obtain this by using union() function.

Python Set Intersection

Again, intersection is the operation to get the common unique elements of two sets. For example, {1, 2, 3, 4} and { 2, 3, 5, 7} are two sets. If we intersect them, we get, {2, 3}. The intersection operation is done by intersection() function.

Python Set Difference

Now, difference operation compares two sets and creates a new set containing items from set A which are not common in set B. Suppose, we have two sets, A = {1, 2, 3, 4} and B = {2, 3, 5, 7}. Then, A – B operation will generate {1, 4}. Moreover, B – A will generate {5, 7}. The difference operation is done by difference() function..

The following code will give you idea about how to do these set operation in python programming.

The output you get will be like this

So, that’s all for today. Hope that you learned well about Python Set. For any further query you can just write your query in the comment box. We will answer you.

Reference: Official Documentation

By admin

Leave a Reply