2. List Overlap

A program that returns a list containing only the common elements between two lists. For example, given the lists [1, 2, 3, 4] and [3, 4, 5, 6], the program should return [3, 4].

image

Solve this code by using β€˜in’ functionality in python list

def find_loop_list():
    final_list = []
    list_1 = (input("Enter first list of number ")).split(' ')
    list_2 = (input("Enter second list of number ")).split(' ')

    for l1 in list_1:
        if l1 in list_2:
            final_list.append(int(l1))
    return final_list

Another way:

def overlap(list1, list2):
    list_overlap = []
    for x in list1:
        for y in list2:
            if x == y and x not in list_overlap:
                list_overlap.append(x)
    return list_overlap
1 Like