0% found this document useful (0 votes)
12 views5 pages

Set Operations

The document discusses various set operations in Python like union, intersection, difference and symmetric difference. It provides examples of using the corresponding methods like union(), intersection(), difference() etc. as well as operators like |, &, - to perform set operations and update sets.

Uploaded by

Lakshmi M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Set Operations

The document discusses various set operations in Python like union, intersection, difference and symmetric difference. It provides examples of using the corresponding methods like union(), intersection(), difference() etc. as well as operators like |, &, - to perform set operations and update sets.

Uploaded by

Lakshmi M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4/10/24, 11:22 AM set operations - Jupyter Notebook

In [ ]: 1 #set operations
2 ​
3 union:
4 #union() method allows you to join a set with other data types,
5 #like lists or tuples.
6 ​
7 #union() and update() will exclude any duplicate items.

In [7]: 1 #union method returns a new set with all items from both sets.
2 set1 = {"a", "b", "c"}
3 set2 = {1, 2, 3}
4 set3 = set1.union(set2)
5 print(set3)
6 ​

{1, 2, 3, 'c', 'b', 'a'}

In [8]: 1 set1 = {"a", "b", "c"}


2 set2 = {1, 2, 3}
3 set3 = set1 | set2
4 print(set3)

{1, 2, 3, 'c', 'b', 'a'}

In [6]: 1 #used to join multiple sets


2 set1 = {"a", "b", "c"}
3 set2 = {1, 2, 3}
4 set3 = {"John", "Elena"}
5 set4 = {"apple", "bananas", "cherry"}
6 myset = set1.union(set2, set3, set4)
7 print(myset)

{1, 2, 3, 'bananas', 'c', 'cherry', 'apple', 'a', 'Elena', 'b', 'John'}

In [5]: 1 # Using | to join multiple sets


2 ​
3 set1 = {"a", "b", "c"}
4 set2 = {1, 2, 3}
5 set3 = {"John", "Elena"}
6 set4 = {"apple", "bananas", "cherry"}
7 myset = set1 | set2 | set3 |set4
8 print(myset)

{1, 2, 3, 'cherry', 'John', 'Elena', 'b', 'bananas', 'a', 'c', 'apple'}

In [6]: 1 #union() method allows you to join a set with other data types
2 ​
3 x = {"a", "b", "c"}
4 y = (1, 2, 3)
5 z = x.union(y)
6 print(z)

{1, 2, 3, 'b', 'a', 'c'}

localhost:8888/notebooks/S SOWMIYA/set operations.ipynb 1/5


4/10/24, 11:22 AM set operations - Jupyter Notebook

In [7]: 1 #The | operator only allows you to join sets with sets,
2 # other data types like you can with the union() method.
3 x = {"a", "b", "c"}
4 y = (1, 2, 3)
5 z=x|y
6 print(z)

--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
Cell In[7], line 4
2 x = {"a", "b", "c"}
3 y = (1, 2, 3)
----> 4 z=x|y
5 print(z)

TypeError: unsupported operand type(s) for |: 'set' and 'tuple'

In [ ]: 1 #Intersection
2 ​
3 #The intersection() method will return a new set, that only contains th
4 #Keep ONLY the duplicates

In [8]: 1 set1 = {"apple", "banana", "cherry"}


2 set2 = {"google", "microsoft", "apple"}
3 ​
4 set3 = set1.intersection(set2)
5 print(set3)

{'apple'}

In [9]: 1 #using & operator


2 ​
3 set1 = {"apple", "banana", "cherry"}
4 set2 = {"google", "microsoft", "apple"}
5 set3 = set1 & set2
6 print(set3)

{'apple'}

localhost:8888/notebooks/S SOWMIYA/set operations.ipynb 2/5


4/10/24, 11:22 AM set operations - Jupyter Notebook

In [11]: 1 #intersection_update() method will also keep ONLY the duplicates,


2 #it will change the original set instead of returning a new set
3 ​
4 set1 = {"apple", "banana", "cherry"}
5 print("set1 before changes",set1)
6 set2 = {"google", "microsoft", "apple"}
7 print("set2 before changes",set2)
8 set1.intersection_update(set2)
9 print("set 1 after changes",set1)
10 print("set 2 after changes",set2)

set1 before changes {'apple', 'banana', 'cherry'}


set2 before changes {'apple', 'microsoft', 'google'}
set 1 after changes {'apple'}
set 2 after changes {'apple', 'microsoft', 'google'}

In [13]: 1 set1 = {"apple", 1, "banana", 0, "cherry"}


2 set2 = {False, "google", 1, "apple", 2, True}
3 set1.intersection_update(set2)
4 print(set1)

{False, 1, 'apple'}

In [ ]: 1 #Difference
2 ​
3 #difference() method will return a new set
4 #that will contain only the items from the first set
5 #that are not present in the other set.
6 ​
7 ​

In [16]: 1 #Keep all items from set1 that are not in set2:
2 set1 = {"apple", "banana", "cherry"}
3 set2 = {"google", "microsoft", "apple"}
4 set3 = set1.difference(set2)
5 print(set3)

{'cherry', 'banana'}

In [17]: 1 set1 = {"apple", "banana", "cherry"}


2 set2 = {"google", "microsoft", "apple"}
3 set3 = set1 - set2
4 print(set3)

{'cherry', 'banana'}

localhost:8888/notebooks/S SOWMIYA/set operations.ipynb 3/5


4/10/24, 11:22 AM set operations - Jupyter Notebook

In [14]: 1 #The difference_update() method will also keep the items from the first
2 #that are not in the other set,
3 #but it will change the original set instead of returning a new set
4 set1 = {"apple", "banana", "cherry"}
5 print("original set",set1)
6 set2 = {"google", "microsoft", "apple"}
7 set1.difference_update(set2)
8 print("difference_update",set1)
9 ​

original set {'apple', 'banana', 'cherry'}


difference_update {'banana', 'cherry'}

In [ ]: 1 #Symmetric Differences
2 ​
3 # will keep only the elements that are NOT present in both sets.
4 ​

In [19]: 1 set1 = {"apple", "banana", "cherry"}


2 set2 = {"google", "microsoft", "apple"}
3 set3 = set1.symmetric_difference(set2)
4 print(set3)

{'google', 'cherry', 'microsoft', 'banana'}

In [20]: 1 set1 = {"apple", "banana", "cherry"}


2 set2 = {"google", "microsoft", "apple"}
3 set3 = set1 ^ set2
4 print(set3)

{'google', 'cherry', 'microsoft', 'banana'}

In [ ]: 1 #symmetric_difference_update()
2 #keep all but the duplicates,
3 #but it will change the original set instead of returning a new set.

In [21]: 1 set1 = {"apple", "banana", "cherry"}


2 set2 = {"google", "microsoft", "apple"}
3 set1.symmetric_difference_update(set2)
4 print(set1)

{'google', 'cherry', 'microsoft', 'banana'}

In [ ]: 1 #issubset()
2 #returns True if all items in the set exists in the specified set,
3 #otherwise it returns False.

localhost:8888/notebooks/S SOWMIYA/set operations.ipynb 4/5


4/10/24, 11:22 AM set operations - Jupyter Notebook

In [22]: 1 #Return True if all items in set x are present in set y


2 x = {"a", "b", "c"}
3 y = {"f", "e", "d", "c", "b", "a"}
4 z = x.issubset(y)
5 print(z)

True

In [23]: 1 x = {"a", "b", "c"}


2 y = {"f", "e", "d", "c", "b", "a"}
3 z = x <= y
4 print(z)

True

In [ ]: 1 #issuperset()
2 #returns True if all items in the specified set exists in the original
3 #otherwise it returns False.

In [24]: 1 #Return True if all items set y are present in set x


2 x = {"f", "e", "d", "c", "b", "a"}
3 y = {"a", "b", "c"}
4 z = x.issuperset(y)
5 print(z)

True

In [25]: 1 x = {"f", "e", "d", "c", "b", "a"}


2 y = {"a", "b", "c"}
3 z = x >= y
4 print(z)

True

In [ ]: 1 #isdisjoint()
2 #returns True if none of the items are present in both sets,
3 #otherwise it returns False.

In [16]: 1 x = {"apple", "banana", "cherry"}


2 y = {"google", "microsoft"}
3 z = x.isdisjoint(y)
4 print(z)

True

In [ ]: 1 ​

localhost:8888/notebooks/S SOWMIYA/set operations.ipynb 5/5

You might also like