Python Practice › Set Programs › Program 26

Update a set to its intersection

EasySet Programs · 26 of 48

Write a Python program to keep only the items shared with another set.

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

Approach

  1. intersection_update() or &=.

Solution

Python
a = {1, 2, 3, 4}a &= {2, 4, 6}print(a)
Output
{2, 4}

Learn the concept

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

More set programs

← All 48 Set Programs

Leave a Comment