Python Program To Find Common Item From Two Set
Chapter:
Python
Last Updated:
27-09-2023 16:39:02 UTC
Program:
/* ............... START ............... */
# Define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# Method 1: Using the intersection method
common_items_method1 = set1.intersection(set2)
# Method 2: Using the & operator
common_items_method2 = set1 & set2
# Display the common items
print("Common items using intersection method:", common_items_method1)
print("Common items using & operator:", common_items_method2)
/* ............... END ............... */
Output
Common items using intersection method: {3, 4, 5}
Common items using & operator: {3, 4, 5}
Notes:
-
In this program, set1 and set2 are two sets, and we find their common items using two different methods: the intersection method and the & operator. The common items will be printed to the console.
Tags
Python program to find common item from two set #Python find common elements in multiple lists