A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Find the intersection (common) of two sets and remove those elements from the first set using python programming
Q:

Find the intersection (common) of two sets and remove those elements from the first set using python programming

0

Find the intersection (common) of two sets and remove those elements from the first set

Given:

first_set = {23, 42, 65, 57, 78, 83, 29}
second_set = {57, 83, 29, 67, 73, 43, 48}

Expected Output:

Intersection is  {57, 83, 29}
First Set after removing common element  {65, 42, 78, 23}

All Answers

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

Hint:

  • Use the intersection() and remove() method of a set

Solution:

  • Get the common items using the first_set.intersection(second_set)
  • Next, iterate common items using a for loop
  • In each iteration, use the remove() method of on first set and pass the current item to it.
first_set = {23, 42, 65, 57, 78, 83, 29}
second_set = {57, 83, 29, 67, 73, 43, 48}

print("First Set ", first_set)
print("Second Set ", second_set)

intersection = first_set.intersection(second_set)
print("Intersection is ", intersection)
for item in intersection:
    first_set.remove(item)

print("First Set after removing common element ", first_set)

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now