Python Practice › Set Programs › Program 25

difference() vs –

EasySet Programs · 25 of 48

Write a Python program to find the difference between two sets with the method and the operator.

Advertisement
Input
{"a", "b", "c"}, {"b"}
Output
['a', 'c'] ['a', 'c']

Approach

  1. difference() accepts any iterable; - needs sets.

Solution

Python
a = {"a", "b", "c"}print(sorted(a.difference(["b"])), sorted(a - {"b"}))
Output
['a', 'c'] ['a', 'c']

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Leave a Comment