Q:

Remove items from set1 that are not common to both set1 and set2 using python programming

belongs to collection: Python Set Exercises

0

Remove items from set1 that are not common to both set1 and set2

Given:

set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

Expected output:

{40, 50, 30}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use the intersection_update() method of a set.

Solution:

set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

set1.intersection_update(set2)
print(set1)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Update set1 by adding items from set2, except comm...