Set & Set Operations In Python. Topic-22

Set & Set Operations In Python

In Python programming, Sets are an unordered collection of data items. In sets, every element is unique. There are no duplicate elements in a set. Sets are defined by placing elements inside curly braces {}, separated by commas.

Create Empty Set:

emp_set = set()
print(emp_set)
Python

The Output Of This Code Will Be:

Output
 set()
Python

Elements In Set

 info = {"Priti", 19, True, 5.9}
 print(info)
Python

The Output Of This Code Will Be:

Output
{True, 19, 5.9, 'Priti'}
Python

Accessing The Sets Item:

Sets are unordered collections. It means there are no orders. So that’s why we can’t use index numbers for accessing the set elements. However, you can iterate through the set using a for loop. Let’s see an example:

info = {"Priti", 19, False, 5.9}
for item in info:
    print(item)
Python

The Output Of This Code Will Be:

Output
False
Priti
19
5.9
Python

Set Operations:

1. Add Elements In The Set:

Using`add()` method we can add new elements in the set.

info = {"first element"}
info.add("New Element")
print(info)
Python

The Output Of This Code Will Be:

Output
{'first element', 'New Element'}
Python

2. Remove Elements From The Set:

info = {"first element", "second element", 19}
info.remove(19)
print(info)
Python

The Output Of This Code Will Be:

Output
{'second element', 'first element'}
Python

3. Union

The union operation combines two sets, keeping only unique elements in the resulting set. In other words, it creates a new set that contains all distinct elements from both sets. Let’s see an example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}

union_set = set1.union(set2)
print("Union:", union_set)
Python

The Output Of This Code Will Be:

Output
Union: {1, 2, 3, 4, 5}
Python

4. Intersection:

The intersection operation contains only the elements that are common in both sets and returns a new set. It identifies the elements that exist in both sets. Let’s see an example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print("Intersection:", intersection_set)
Python

The Output Of This Code Will Be:

Output
Intersection: {3}
Python

5. Difference:

The difference operation returns a new set containing the elements that are present in the first set but not in the second set. It identifies the elements unique to the first set. Let’s see an example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print("Difference:", difference_set)
Python

The Output Of This Code Will Be:

Output
Difference: {1, 2}
Python

4. symmetric_difference()

This method calculates the symmetric difference between two sets, meaning it returns the elements that are present in either of the sets, but not in both. It creates a new set containing these unique elements. Let’s perform this operation:

cities = {"Kolkata", "Lucknow", "Chennai", "Delhi"}
cities2 = {"Kolkata", "Seoul", "Bangalore", "Lucknow"}

cities3 = cities.symmetric_difference(cities2)
print(cities3)
Python

The Output Of This Code Will Be:

Output
{'Seoul', 'Bangalore', 'Chennai', 'Delhi'}
Python

Here, `cities3` contains only cities that are unique to either of the sets.

5. symmetric_difference_update():

This method also calculates the symmetric difference, but it updates the existing set with the result. It does not create any new set or update any existing set. Let’s see an example:

cities = {"Kolkata", "Lucknow", "Chennai", "Delhi"}
cities2 = {"Kolkata", "Seoul", "Bangalore", "Lucknow"}

cities.symmetric_difference_update(cities2)
print(cities)
Python

The Output Of This Code Will Be:

Output
{'Bangalore', 'Delhi', 'Chennai', 'Seoul'}
Python

Here, cities contain cities that are unique to either of the sets, and the existing set is updated with these changes.

6. `difference()`:

The `difference()` method in Python sets is used to find the set of elements that are present in the original set but not in another set. It returns a new set containing the unique elements that exist only in the original set. Let’s see an example:

cities = {"Kolkata", "Chennai", "Berlin", "Delhi"}
cities2 = {"Seoul", "Siliguri", "Delhi"}

cities3 = cities.difference(cities2)
print(cities3)
Python

The Output Of This Code Will Be:

Output
{'Kolkata', 'Chennai', 'Berlin'}
Python

Here, `cities3` contains cities that are present in the `cities` set but not in the `cities2` set.

7. `difference_update()`:

The `difference_update()` method is similar, but it updates the existing set with the result instead of creating a new set. It modifies the original set to contain only the elements that are unique to it. Let’s see some examples:

cities = {"Kolkata", "Chennai", "Berlin", "Delhi"}
cities2 = {"Seoul", "Siliguri", "Delhi"}

cities.difference_update(cities2)
print(cities)
Python

The Output Of This Code Will Be:

Output
{'Kolkata', 'Berlin', 'Chennai'}
Python

Here, `cities` is updated to contain only the cities that are unique to it, and the modification is done in place.

If you don’t know the Recursion in Python” just click on Read More.

I hope you understand “Set & Set Operations In Python”  Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

ABOUT ME
Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog