Sunday, 5 July 2026

πŸš€ Day 82/150 – Set Operations (Union & Intersection) in Python

πŸš€ Day 82/150 – Set Operations (Union & Intersection) in Python

Sets are one of Python's most useful data structures for working with unique values. They support powerful mathematical operations like union and intersection, making it easy to combine or compare collections of data.

In this post, we'll explore four different ways to perform set operations in Python.


Method 1 – Using Union (|) Operator

The | operator combines all unique elements from two sets.

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




Output:

{1, 2, 3, 4, 5}

Explanation:
  • The union operation returns every unique element from both sets.
  • Duplicate values appear only once.

Method 2 – Using union() Method

You can also use the built-in union() method to merge sets.

set1 = {10, 20, 30} set2 = {30, 40, 50} result = set1.union(set2) print(result)





Output:

{10, 20, 30, 40, 50}

Explanation:
  • union() works the same as the | operator.
  • It returns a new set without modifying the original sets.

Method 3 – Using Intersection (&) Operator

The & operator returns only the elements that exist in both sets.

set1 = {"apple", "banana", "mango"} set2 = {"banana", "grapes", "apple"} result = set1 & set2 print(result)




Output:

{'apple', 'banana'}

Explanation:

  • Only common elements are included.
  • Elements that exist in only one set are ignored.

Method 4 – Using intersection() Method

The intersection() method provides another way to find common elements.

set1 = {2, 4, 6, 8} set2 = {1, 2, 3, 4} result = set1.intersection(set2) print(result)







Output:
{2, 4}

Explanation:

  • Returns a new set containing only common values.
  • The original sets remain unchanged.

Comparison of Methods

MethodBest For
`` Operator
union()Readable union operation
& OperatorQuick intersection
intersection()Readable intersection operation

πŸ”₯ Key Takeaways

✅ Sets automatically remove duplicate values.

✅ Use the | operator or union() to combine unique elements from multiple sets.

✅ Use the & operator or intersection() to find common elements between sets.

✅ Set operations are fast and efficient for comparing collections of data.

✅ They are widely used in data analysis, filtering, and solving algorithmic problems.

 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (300) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (12) BI (10) Books (270) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (7) Data Analysis (38) Data Analytics (26) data management (16) Data Science (382) Data Strucures (23) Deep Learning (187) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (74) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (335) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1397) Python Coding Challenge (1178) Python Mathematics (4) Python Mistakes (51) Python Quiz (559) Python Tips (22) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)