Join two Sets in Python (Union, Intersection, Difference, Symmetric) - Python Tutorial for Beginners

Published: 14 April 2021
on channel: Digital Academy
1,328
19

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ How to Join two Sets in Python? (Union, Intersection, Difference, Symmetric)

Sets are commonly used for Computing Mathematical Operations in Python, such as intersection, union, difference, and symmetric difference. Consequently, there are multiple ways to join two sets in Python.

○ Union of two Sets in Python

Union of the sets A and B is the set of all items in either A or B. You can use the method union(), that returns a new set containing all items from both sets, or the method update(), that inserts all the items from one set into another set. You can perform union on 2 sets using the method union() in Python, or "|" operator.

A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}

A.update(B)
my_new_set = (A | B)
my_new_set = A.union(B)
my_new_set = {'blue', 'green', 'yellow', 'orange', 'red'}

There are other methods that joins two sets and keeps ONLY the duplicates or, on the opposite, NEVER keeps the duplicates...


○ Intersection of two Sets in Python

Intersection of the sets A and B is the set of items common to both A and B. And you can perform intersection on two sets using the method intersection() in Python, or "&" operator.

A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}

my_new_set = (A & B)
my_new_set = A.intersection(B)
my_new_set = {'red'}


○ Difference of two Sets in Python

Set Difference of A and B is the set of all items that are in A but not in B. You can compute the difference between two sets using the method difference(), or "-" operator.

A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}

my_new_set = (A - B)
my_new_set = A.difference(B)
my_new_set = {'blue', 'green'}


○ Symmetric Difference of two Sets in Python

Symmetric difference of sets A and B is the set of all elements in either A or B, but not both. You can compute symmetric difference between two sets using symmetric_difference() in Python, or "^" operator.

A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}

my_new_set = (A ^ B)
my_new_set = A.symmetric_difference(B)
my_new_set = {'orange', 'blue', 'green', 'yellow'}


Let's play this video, stick around and watch until the end of this video! 👍🏻

- Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python -    • DATA TYPES in Python (Numbers, String...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assi...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF,...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Co...  

📖 Blog: http://digital.academy.free.fr/blog

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***


Watch video Join two Sets in Python (Union, Intersection, Difference, Symmetric) - Python Tutorial for Beginners online without registration, duration hours minute second in high quality. This video was added by user Digital Academy 14 April 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,32 once and liked it 1 people.