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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started