Set Intersection Function

Consider two lists of integers, where each list is a set (i.e., contains no duplicate values). The intersection of these two sets will be a list containing only those integers that appear in both of the source lists/sets. For example, given the lists [1, 2, 3, 4, 5] and [2, 4, 6, 8, 10], their intersection would be the list [2, 4] (note that the source lists and the intersection set do not need to contain values in sorted order).
Complete the intersection() function, which takes two integer lists as its arguments. This function returns a Python list whose only elements are those integers that appear in both of the list parameters. If the two source lists have no elements in common, then your function should return an empty list.

Function Call
intersection([1,2,3,4,5], [2,4,6,8,10])
Return Value
[2, 4]
Function Call
intersection([12,45,91], [36,91])
Return value
Function Call
intersection([2,3,4,5,6], [7,8,9,10,11])
Return value

Please help I don’t know how to write out the function !!!


given, a = [1,2,3,4,5,6], and, b = [2,4,6,7,8,9]

the intersection is the set, {2, 4, 6}. Of course we can just inspect the lists visually and determine this much. How do we capture this?

c = list(set(a).intersection(b))

Mind, I probably just took away all the joy of learning this on your own, but it only took me a couple of minutes, so…

If we rule out the built in set() function, then it will take a little more logic. Not insurmountable, though. Put on your thinking cap.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.