It is a method in python that returns a set containing the elements that are there in the first set but not in the second set. It doesn’t modifies the given set.
Syntax:
set1.difference(set2)
or
set1-(set2)
Also Read: Python Set – intersection_update() Method
Example 1:
# Python code to understand difference() method in set
# www.codewindow.in
s1 = {"code", "window"}
s = {"code", "website"}
#union of s and s1?
print(s1.difference(s))
Output:
{'window'}
Explanation:
Here, it returned a set with all the common elements removed from the first set s1 and set s. “code” is present in both the set, so it removed that element from set s1 and returned a new set containing only window.
Example 2:
# Python code to understand difference() method in set
# www.codewindow.in
s1 = {2, 5, 8, 7, 9}
s = {10, 5, 2}
#difference of s and s1
print(s.difference(s1))
#diffrerence of s1 and s
print(s1-(s))
Output:
{10}
{8, 9, 7}
Explanation:
Here, first it returned a set which has all the common elements removed from the first set (s) and set s1.
Second, it returned a set which has all the common element removed from the first set (s1) and set s.
Follow Us
You Missed
- Apple Is Hiring | For DevOps Engineer – codewindow.in
- L’Oréal is Hiring | Graduate Engineer Trainee – codewindow.in
- Zoho is Hiring | Web Developers – codewindow.in
- TATA Electronics is Hiring | Graduate Engineer Trainee – codewindow.in
- Wipro WILP 2022 – codewindow.in
- Harman is Hiring | Associate Engineer, SW – codewindow.in
- NVDIA Interview Questions – codewindow.in
- Latenview Analytics Overall Interview Questions + Coding Solutions – codewindow.in
- Adobe Overall Interview Questions + Coding Solutions – codewindow.in
- Linkedin Overall Interview Questions + Coding Solutions- codewindow.in
- AXA Insurance is Hiring | Trainee – Global Technology – codewindow.in
- Accenture is Hiring | Data Analyst – codewindow.in
- PWC Interview Questions + Coding Solutions – codewindow.in
- ITC Infotech Overall Interview Questions – codewindow.in
- Texas Instrument Interview Questions + Coding Solutions – codewindow.in
- Deutsche Bank Interview Questions + Coding Solutions – codewindow.in
- Optum – Overall Interview Questions + Coding Solutions – codewindow.in
- Salesforce – Overall Interview Questions + Coding Solutions – codewindow.in
- Morgan Stanly Interview Questions + Coding Solutions – codewindow.in
- Flipkart – Overall Interview Questions + coding Solutions- codewindow.in
- Amazon Interview Questions + Coding Solutions – codewindow.in
- Code Nation – Overall Interview Questions + coding Solutions – codewindow.in
- Bridgei2i – Overall Interview Questions + Coding Solutions – codewindow.in
- De Shaw Interview Questions + Coding Solutions – codewindow.in
- Tiger Analytics – Overall Interview Questions + Coding Solutions – codewindow.in
- Incture Interview Questions +Coding Solutions – codwindow.in
- Commvault Systems Overall Interview Questions + Coding Solution – codewindow.in
- Teg Analytics Overall Interview Questions + Coding Solutions – codewindow.in
- Celebal Tech Interview Questions + Coding Solutions – codewindow.in
- Smart Cube Interview Questions – codewindow.in
- Exxon Mobil Overall Interview Questions – codewindow.in
- Siemens Overall Interview Questions + Coding Solutions – codewindow.in
Also Checkout
- Adobe
- Algorithm
- Amagi
- Amazon Interview Questions
- Aptitude
- Books
- Bridge2i
- Campgemini Interview Questions
- Capgemini Coding Questions
- Capgemini Pseudocode
- Celebal Tech
- code nation
- CodeVita
- Coding Questions
- Cognizant Placement
- commvault Systems
- CTS
- Data Structure and Algorithm
- De Show Interview Questions
- deloitte
- Deutsche Bank Interview questions
- Epam Full Question Paper
- Exxon Mobil interview questions
- filpkart
- Fractal Analytics Interview Questions
- Genpact
- Grab
- GreyB Interview Questions
- Guidance for Accenture
- Gupshup
- HCL Interview Questions
- Hexaware
- HFCL
- HR Questions
- IBM Interview questions
- IBM Questions
- Incture Interview Questions
- Infineon Technologies Interview Questions
- Infosys
- Infosys Interview Questions
- Internship
- Interview Experience
- Interview Questions
- ITC Infotech
- itron
- JECA
- Job Info
- Kantar Interview Questions
- Larsen & Turbo
- Latenview AnalyticsInterview questions
- Lexmark International Interview Questions
- Machine Learning
- Media.net
- Mindtree Interview Questions
- Miscellaneous
- Morgan Stanly Interview Questions
- nagarro
- navi
- NPCI
- NTT Data Interview Questions
- NVDIA
- NVDIA interview questions
- Optum
- PayU
- Persistent INterview Questions
- Programming in C
- Programming in C++
- Programming in JAVA
- Programming in Python
- pseudocode
- PWC Interview Questions
- Quiz
- Razorpay
- Recruiting Companies
- Revature
- salesforce
- Samsung
- Schlumberger
- Seimens
- Slice
- Smart Cube
- Study Material
- Tally Solutions
- tata cliq
- TCS
- TCS NQT
- TCS NQT Coding Questions
- Tech Mahindra Coding Questions
- Tech Mahindra Questions
- Teg Analytics
- Tejas Network Interview Questions
- Texas Instrument Interview Questions
- Tiger Analytics
- Uncategorized
- UnDosTres
- Unstop
- Verbal Ability
- Web Development
- Wipro Coding Questions
- Wipro interview Questions
- Wipro NLTH
- WIpro NLTH Coding Solve
- Zenser
- Zoho Interview Questions