What is difference method in sets | How to use difference method of sets?

Definition:

This method is used to return another set which is in A and not in B, meaning, A – B is applied and whatever remains in A will be passed into another variable. Also, the original set remains the unchanged.

difference

Syntax:

Variable = set1.difference(set2)

This method takes an argument.

Working:

What is method in sets in python

output

Code:

  1. A set with some values is taken
  2. Another set with some common values in set A is taken.
  3. difference() method on set A is applied with B and that value is passed to C.
  4. The value of C is printed

Output:

  1. A set with A-B is printed where we cannot see common items of A & B.

Interview Questions:

  • How to remove common items from first set and don’t want to print the data in 3rd variable, we want changes in original set itself.

To achieve this kind of changes there is a method called difference_update().

Quick Topics:

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *