Introduction to Python Programming Cycle of Python Varibles and Datatypes in Python Input and Output in Python Operators in Python Precedence of Operators in Python Typecasting in Python If Else in Python Loops in Python Break, Continue and Pass in Python Functions in Python Default Arguments and Keyword Arguments in Python Strings in python Lists in Python Tuple in Python Dictionary in Python Sets in Python List Comprehension in Python Unpacking Sequences in Python Higher Order Functions in Python Lambda Functions in Python Sieve of Eratosthenes Algorithm in Python Linear Search in Python Binary Search in Python Selection Sort in Python Bubble Sort in Python Insertion Sort in Python Merge Sort in Python

Sets in Python

Jump to specific section:
  1. Create Set
  2. Iterate Over Set
  3. Set Methods
  4. Union of Sets
  5. Intersection of Sets
  6. Difference of Sets
  7. Symmetric Difference of Sets

Set in Python is a data type that is used to store unique elements, which means repetition is not allowed. Sets are basically an unordered collection of values without duplicate values. Set object of python support mathematical operations like union, intersection, difference, and symmetric difference.

How to Create Set?

We use curly braces or the set() function to create a set. Consider the following code:

# create a non-empty set
set1 = {'one','two','three','four'}
print(type(set1))
print(set1)

print()

# Create empty set
set2 = set()
print(type(set2))
print(set2)
            
Output
<class 'set'>
{'two', 'three', 'four', 'one'}

<class 'set'>
set()                 

We can iterate over a set using for loop in the following way:

set1 = {"Rapid","Coders","Akshay"}

for i in set1:
    print(i)
Output:
Rapid
Akshay
Coders

Set methods:

  1. set.add(value): It inserts value in set.
  2. set.update([list]): It insert values of given list in set.
  3. set.discard(value): It removes value from the set if it is present. If the value is not present then it will not raise an error.
  4. set.remove(value): It removes a value from the set if it is present and raises an error if a value is not present.
  5. set.clear(): It deletes all values of set.
  6. set.pop(): It removes the last value from the set, but the set is unordered so we can not determine which value will be deleted. So we can say it removes a random value from the set.

Consider following code:

set1 = {"Rapid","Coders","Akshay"}
print("Original Set")
print(set1)
print()

set1.add("Singh")
print("Add one new value in set using add funtion")
print(set1)
print()

# Create list and add its element to set using update funtion
lst = ["We","are","coders"]
set1.update(lst)
print("After adding list elements")
print(set1)
print()

# Remove 'are' using discard funtion
set1.discard("are")
print("After discarding 'are'")
print(set1)
print()

# Remove 'Singh' using remove funtion
set1.remove("Singh")
print("After removing 'Singh'")
print(set1)
print()

# Remove random value using pop funtion 
set1.pop()
print("Removing random value")
print(set1)
print()

# Delete all value using clear funtion 
set1.clear()
print("Deleting all values of set")
print(set1)
print()
Output:
Original Set
{'Coders', 'Akshay', 'Rapid'}

Add one new value in set using add funtion
{'Coders', 'Akshay', 'Rapid', 'Singh'}

After adding list elements
{'are', 'Rapid', 'Singh', 'coders', 'We', 'Coders', 'Akshay'}

After discarding 'are'
{'Rapid', 'Singh', 'coders', 'We', 'Coders', 'Akshay'}

After removing 'Singh'
{'Rapid', 'coders', 'We', 'Coders', 'Akshay'}

Removing random value
{'coders', 'We', 'Coders', 'Akshay'}

Deleting all values of set
set()
                

Union of Sets

We use the union() function of set to find union of 2 sets. Union of two sets is a set that contains all values of both sets, if any value occurs more than one time then it is considered only once. Consider the following example:


set1 = {"Rapid","Coders"}
set2 = {"Akshay","Rapid"}

# union of set 1 and set 2
set3 = set1.union(set2)
print(set3)
                
Output
{'Akshay', 'Rapid', 'Coders'}               
    

Intersection of Sets

We use the intersection() function of set to find intersection of 2 sets. The intersection of two sets is a set that contains only common values of both sets. Consider the following example:


set1 = {"Rapid","Coders"}
set2 = {"Akshay","Rapid"}

# intersection of set 1 and set 2
set3 = set1.intersection(set2)
print(set3)
                    
Output
{'Rapid'}              
        

Difference of Sets

We use the difference() function of set to find difference of 2 sets. The difference of two sets is a set that contains all values of set 1 which are not in set 2. Consider the following example:


set1 = {"Rapid","Coders"}
set2 = {"Akshay","Rapid"}

# set3 = set1 - set2
set3 = set1.difference(set2)
print(set3)
                    
Output
{'Rapid'}              
        

Symmetric Difference of Sets

We use the symmetric_difference() function of set to find symmetric difference of 2 sets. Symmetric difference of two sets is a set that contains values that are either in set 1 or in set 2 but not in both sets. Consider the following example:


set1 = {"Rapid","Coders"}
set2 = {"Akshay","Rapid"}

# symmetric difference of set 1 and set 2
set3 = set1.symmetric_difference(set2)
print(set3)
                    
Output
{'Akshay', 'Coders'}