Python Set Intersection | Find Common Items in Sets with Examples

Python Set Intersection

The intersection of sets means finding common elements between two or more sets. In Python, you can use the intersection() method or the & operator to find common items.

Set का intersection का मतलब होता है दो या अधिक sets के बीच common elements निकालना। Python में intersection() method या & operator का इस्तेमाल किया जाता है।

Methods to Find Intersection

  • set1.intersection(set2): Returns a new set with common items.
  • set1 & set2: Shorthand operator for intersection.
  • set1.intersection_update(set2): Updates the set in place with common items.
  • set1.intersection(set2): एक नया set return करता है जिसमें common items होते हैं।
  • set1 & set2: intersection के लिए shorthand operator।
  • set1.intersection_update(set2): set को inplace update करता है सिर्फ common items से।
Example 1: Basic Intersection Using intersection()
a = {1, 2, 3}
b = {2, 3, 4}
print(a.intersection(b))

Find common elements between two sets using intersection().

intersection() से दो sets के common elements निकालें।

Output:आउटपुट:

{2, 3}
Example 2: Using '&' Operator
a = {10, 20, 30}
b = {20, 30, 40}
print(a & b)

Use & operator to find intersection.

& operator से intersection निकालें।

Output:आउटपुट:

{20, 30}
Example 3: Intersection of Three Sets
a = {1, 2, 3}
b = {2, 3, 4}
c = {3, 4, 5}
print(a.intersection(b, c))

Find common elements among three sets.

तीन sets के बीच common element निकालें।

Output:आउटपुट:

{3}
Example 4: Using intersection_update()
a = {1, 2, 3, 4}
b = {2, 4, 6}
a.intersection_update(b)
print(a)

Update set a with only the common elements using intersection_update().

intersection_update() से set a को सिर्फ common elements से update करें।

Output:आउटपुट:

{2, 4}
Example 5: No Common Elements
a = {1, 2}
b = {3, 4}
print(a.intersection(b))

If there are no common elements, result is an empty set.

अगर कोई common element नहीं है, तो empty set मिलेगा।

Output:आउटपुट:

set()
Example 6: Intersection with Strings
a = set('hello')
b = set('world')
print(a & b)

Find common characters between two strings by converting them to sets.

दो strings को set में बदलकर उनके common characters निकालें।

Output:आउटपुट:

{'o', 'l'}
Example 7: Intersection with Range
a = set(range(1, 6))
b = set(range(4, 9))
print(a.intersection(b))

Find common numbers between two ranges.

दो ranges के बीच common numbers निकालें।

Output:आउटपुट:

{4, 5}
Example 8: Intersection with Duplicate Values
a = {1, 1, 2, 2, 3}
b = {2, 2, 3, 3}
print(a & b)

Sets ignore duplicates, so intersection works on unique items only.

Sets duplicates ignore करते हैं, इसलिए intersection सिर्फ unique items पर काम करता है।

Output:आउटपुट:

{2, 3}
Example 9: Using Loop to Find Intersection
a = {1, 2, 3}
b = {2, 3, 4}
common = {x for x in a if x in b}
print(common)

Find intersection manually using loop comprehension.

loop comprehension से manually intersection निकालें।

Output:आउटपुट:

{2, 3}
Example 10: Intersection with Different Data Types
a = {1, 'a', (2, 3)}
b = {'a', (2, 3), 5}
print(a & b)

Intersection can work with mixed data types, as long as they are hashable.

Intersection mixed data types पर भी काम करता है, बशर्ते वे hashable हों।

Output:आउटपुट:

{'a', (2, 3)}