Python Practice › Set Programs › Program 8

Difference of two sets

EasySet Programs · 8 of 48

Write a Python program to find the items in the first set but not the second.

Advertisement
Input
{1, 2, 3, 4}, {3, 4, 5}
Output
{1, 2} {5}

Approach

  1. - or difference(); the order of the sets matters.

Solution

Python
a, b = {1, 2, 3, 4}, {3, 4, 5}print(a - b, b - a)
Output
{1, 2} {5}

Learn the concept

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

More set programs

← All 48 Set Programs

Leave a Comment