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
- Dunzo Off-Campusing Drive | Data Engineer | codewindow.in
- Verbal Ability Questions Solved | Synonyms Antonyms – codewindow.in
- Verbal Ability Questions Solved | Statement Correction – codewindow.in
- Verbal Ability Questions Solved | Spotting Error – codewindow.in
- Verbal Ability Questions Solved | Speech and Tenses – codewindow.in
- Try
- Verbal Ability Questions Solved | Sentence Arrangement – codewindow.in
- Verbal Ability Questions Solved | Prepositions – codewindow.in
- Verbal Ability Questions Solved | Grammar – codewindow.in
- Verbal Ability Questions Solved | Articles – codewindow.in
- Zoho Off Campus Drive | Product Marketing Associate / Specialist | codewindow.in
- BerryWorks Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- Microsoft Off Campus Hiring Drive – Technical Support – Codewindow.in
- Infosys Pseudocede & Puzzle Solved – codewindow.in
- Infosys Verbal Questions Solved – codewindow.in
- Amazon Off Campus Drive | Trainee | codewindow.in
- Hawkins Off Campus Drive | Management trainees | codewindow.in
- Infosys Mathematical Questions Solved – codewindow.in
- Paypal Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- IBM Off Campus Hiring Drive – Associate Systems Engineer – Codewindow.in
- Infosys Off Campus Hiring Drive – SP and DSE – Codewindow.in
- VIRTUSA Recruitment 2022 | Associate Engineer | codewindow.in
- Infosys Logical Questions solved – codewindow.in
- Google Off Campus Hiring Drive – IT Support Engineer – Codewindow.in
- Capgemini Pseudocode Solved | Set 4 – codewindow.in
- Capgemini Pseudocode Solved | Set 3 – codewindow.in
- Capgemini Pseudocode Solved | Set 2 – codewindow.in
- JECA 2022 Question Paper – 90+ Sample Questions Exposed – codewindow.in
- Capgemini Pseudocode Solved | Set 1 – codewindow.in
- Verizon Off Campusing | Software Developer | codewindow.in
- TCS Recruitment Off-Campusing | Service Desk Analyst | codwindow.in
- Amdocs Off-Campusing | Devops Engineer | codewindow.in
Also Checkout
- Algorithm
- Aptitude
- Capgemini Coding Questions
- Capgemini Pseudocode
- CodeVita
- Coding Questions
- Cognizant Placement
- Data Structure and Algorithm
- Epam Full Question Paper
- Guidance for Accenture
- HR Questions
- IBM Questions
- Infosys
- Internship
- Interview Experience
- JECA
- Job Info
- Machine Learning
- Miscellaneous
- NPCI
- Programming in C
- Programming in C++
- Programming in JAVA
- Programming in Python
- Quiz
- Recruiting Companies
- Revature
- Study Material
- TCS NQT
- Tech Mahindra Coding Questions
- Tech Mahindra Questions
- Uncategorized
- Verbal Ability
- Web Development
- Wipro Coding Questions
- Wipro NLTH
- WIpro NLTH Coding Solve