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 .
Contents
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.
1 2 3 4 |
my_set = set([6,9,8,0]) print(" Set : ",my_set) |
On running you will get such output.
- 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.
1 2 3 4 |
my_set = {6,9,8,0} print(" Set : ",my_set) |
- On running you will get following result.
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.
1 2 3 4 5 6 |
# set created with mixed data types my_set = {'set in python', 50.05, 1000} print(" Set : ",my_set) |
- Now run this code, you will following output.
Creating Empty Set
For creating empty set you have to use set( ) method without any argument.
1 2 3 4 |
my_set = set() print(" Set : ",my_set) |
Result
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.
1 2 3 4 |
my_set = set([1,5,6,7,8,5,6,7]) print(" Set : ",my_set) |
In result you can see that the duplicate values have been removed. Let’s see –
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.
1 2 3 4 5 6 |
set_1 = set([1,9,4,8]) print("Original set :", set_1) set_1.add(3) print("\n final set :",set_1) |
- 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.
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.
1 2 3 4 5 6 7 |
set_1 = set([1,9,4,8]) print("Original set :", set_1) set_1.remove(9) print("\n final set :",set_1) |
Result
- 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.
1 2 3 4 5 6 7 |
set_1 = set([1,9,4,8]) print("Original set :", set_1) set_1.discard(2) print("Discarded set",set_1) |
Now see the result –
- You can see the set is remain unchanged.
But if you use remove() method then see what will happened.
- write the following code.
1 2 3 4 5 6 |
set_1 = set([1,9,4,8]) set_1.remove(2) print("\n final set :",set_1) |
Now run it, you will get an error.
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.
1 2 3 4 5 6 7 8 |
set_1 = set(["Jash", "Babes", "Dolphi"]) print("\n Original set :",set_1) # pass a list of values to the update() method set_1.update(["Marie", "John", "Soma"]) print("\n updated set :",set_1) |
Now see the output.
Updating set with another set
You can also update your set with another set. let’s understand it with an example.
1 2 3 4 5 6 7 8 9 10 |
set_1 = set(["Jash", "Babes", "Dolphi"]) print("\n Original set :",set_1) # Define a new set set_2 = set(["Marie", "John", "Jimi","Rohan"]) set_1.update(["Marie", "John", "Soma"],set_2) # It will update value from the list and the set print("\n updated set :",set_1) |
Now run the code and see the result.
- 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.
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.
1 2 3 4 5 6 7 8 |
set_1 = set([9,2,3]) # create set1 set_2 = set([8,2,5,6]) # create set2 set_3 = set_1.union(set_2) # union of set1 and set2 print("\n union of set :",set_3) |
Result
Set Intersection Operation
Intersection of sets means a set of common elements from both sets. let’s see it with an example.
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.
1 2 3 4 5 6 7 8 |
set_1 = set([9,2,3,8]) set_2 = set([8,2,5,6]) set_3 = set_1.intersection(set_2) # intersection of set1 and set2 print("\n Intersection of set :",set_3) |
Now see the result –
- 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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
set_1 = set([9,2,3,8]) set_2 = set([8,2,5,6]) # Difference of set1 and set2 set_3 = set_1.difference(set_2) print("\n Set Difference :: A - B :",set_3) # Difference of set2 and set1 set_3 = set_2.difference(set_1) print("\n Set Difference :: B - A:",set_3) |
Now see the result –
Related Articles :
- Merge Sort Python Tutorial – An Efficient Way Of Sorting
- Python Binary To Decimal Tutorial With Examples
- Python Sort Dictionary By Value – Sort With Lambda Function And Operator Module
- Sublime Run Python – Running Python Programs On Sublime Text 3
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