Sets In Python Tutorial For Beginners

As we know there are many data structures in python like list, dictionary, tuple, sets etc but in this post we will talk about sets data structure in python. In this tutorial you will learn what is set in python, what are the use of sets in python, how to create it and many more. So lets start Sets In Python .

Sets Data Structure In Python

So first of all, we will see definition of sets in python and then move further.

What Is Set In Python ?

  • A set is an unordered collection of data type that is mutable.
  • Each element in a set is unique.

Why We Use Set ?

  • Since set is unordered collection of data but also mutable, so it is very helpful working with huge datasets.
  • It has a highly optimized method for checking whether a specific element is contained in the set.

Sets  In Python – How To Create Sets In Python

Now we will learn how to create set in python. We can create set in python using either set() function or curly braces { }.

Creating set using set( ) function

For creating set using set( ) method, simply you have to pass a list of values to the set( ) function.

  • Write the following code and run it.

On running you will get such output.

Sets In Python
Sets In Python
  • Its look like a list but have curly braces { } instead of square bracket [ ]
  • You have noticed one thing that is, the order of elements have been changed in the result. The reason behind this is because set is an unordered collection of data and it doesn’t care about order of elements like lists and tuples .

Creating set using curly braces { }

Creating set using curly braces is very easy.

  • Simply you have to put list of values inside the curly braces separated with comma.

  • On running you will get following result.
Sets In Python
Sets In Python

Set With Mixed Data Types

You can also create a set having any number of elements and it may be any kind of type like integer, float, string etc.

  • let’s see it with an example.

  • Now run this code, you will following output.
Sets In Python
Sets In Python

Creating Empty Set

For creating empty set you have to use set( ) method without any argument.

Result

Sets In Python
Sets In Python

Remember one thing, you can’t create empty set using curly braces { } because empty curly braces make empty dictionary in python.

Set Removes Duplicate Values

Set also remove duplicate values. let’s see it with an example.

  • Create a set with duplicate values.

In result you can see that the duplicate values have been removed. Let’s see –

Sets In Python
Sets In Python

Sets In Python – How To Manipulate Sets In Python

Just like with a list we can also manipulate set such as adding/removing/updating value to the set. Let’s see them with examples.

Adding Values To Set

Now if you want to add more values to the set you can do that by calling add( ) method. Let’s see an example.

  • By using add( ) method you can add only one value to the set but if you want to add multiple values then you have to use update( ) method.

Now run this code, you will get below output.

Sets In Python
Sets In Python

Removing Values From The Set

You can remove value from the set using remove( )  or discard( ) method.

remove( ) method

Write the following code and run it.

Result

Sets In Python
Sets In Python
  • Now you can see that 9 has been removed from the set.

discard( ) method

discard() method is also used to remove value from the set but the difference between discard() and remove() method is that –

  • if you use discard() method and the item does not exist in the set, it remains unchanged.
  • But if you use remove() method an error(key error) will be raised  in such condition.

Let’s see it with an example.

Now see the result –

Sets In Python
Sets In Python
  • You can see the set is remain unchanged.

But if you use remove() method then see what will happened.

  • write the following code.

Now run it, you will get an error.

Sets In Python
Sets In Python

Updating The Set

You can update your set using update( ) method. Using update( ) method you can add multiple values to your set at one time. Let’s see it with examples.

  • Write the following code.

Now see the output.

Sets In Python
Sets In Python

Updating set with another set

You can also update your set with another set. let’s understand it with an example.

Now run the code and see the result.

Sets In Python
Sets In Python
  • You can see the set is updated with the list of values and the set, and the duplicate values have been removed as usual because it is a set.

Sets In Python – Python Set Operations

In this section we will learn how to perform different operations on set in python. We can do the following operations on set –

  • Union
  • Intersection
  • Difference
  • Symmetric difference

Set Union Operation

first of all we have to understand what is union operation. Union of two sets means a set of all elements from both set. let’s see it with an example.

Sets In Python
Sets In Python

In the above figure, there is two set A and B so the union of set A and B will be the set of all elements from both sets.

  • Write the following code and run it.

Result

Sets In Python
Sets In Python

Set Intersection Operation

Intersection of sets means a set of common elements from both sets. let’s see it with an example.

Sets In Python
Sets In Python

In the above figure, there is two set A and B so the intersection of set A and B will be the set of common elements from both sets.

  • Write the following code.

Now see the result –

Sets In Python
Sets In Python
  • You can see only 2 and 8 are printed because 2 and 8 are present in both sets.

Set Difference Operation

Set difference means the set of elements which are only in one set but not in another set.

Sets In Python
Sets In Python

In the above figure, A and B are two sets. So difference of A and B (A-B) is the set of elements that are in A but not in B. Similarly (B-A) is the set of elements that are in set B but not in set A. Let’s see it with an example.

  • Set difference operation can be done using difference( ) method.

Now see the result –

Sets In Python
Sets In Python

Related Articles :

 

So guys this was all about Sets In Python Tutorial. If you have any doubt then please feel free to comment. And the last thing don’t forget to share with your friends. Thanks

Leave a Comment