Question: Create a function that takes two lists (lst1 and lst2), and finds all items in lst2 that also occur in lst1. Return all overlapping items

Create a function that takes two lists (lst1 and lst2), and finds all items in lst2 that also occur in lst1. Return all overlapping items in a list, whose items are in the same order as lst2 (i.e. items appear earlier in lst2 should also appear earlier in the returned list).

Note that this function should preserve all intersecting values as many times as they occur in both lists.

Examples (lst1, lst2 -> return):

[True, True], [False, False] -> []

[a, b, c, c], [c, c, d, e] -> [c, c]

[100, 200, 200, 300], [200, 300, 200, 200] -> [200, 300, 200]

[101.5], [101.5, 101.5, 101.5] -> [101.5]

[d, u, u, p], [p, u, u, u] -> [p, u, u]

CODE:

def find_intersection(lst1, lst2):

"""

>>> find_intersection([1, 2, 3, 4], [1, 2, 1, 2])

[1, 2]

>>> find_intersection(['a', 'b', 'c', 'c'], ['c', 'c', 'd', 'e'])

['c', 'c']

>>> find_intersection([1, 2, 3], [])

[]

"""

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!