Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – First occurrence of one list in another

Improve Article
Save Article
  • Last Updated : 03 Feb, 2023
Improve Article
Save Article

Given two lists, the task is to write a Python program to extract the first element that occurs in list 1 from list 2.

Examples:

Input : test_list1 = [1, 6, 3, 7, 8, 9, 2], test_list2 = [4, 10, 8, 2, 0, 11]

Output : 8

Explanation : 8 is first element from list 2, that occurs in list 1, in 5th index.

Input : test_list1 = [1, 6, 3, 7, 8, 9, 2], test_list2 = [4, 10, 18, 12, 0, 11]

Output : None

Explanation : No element of list 2 found in list 1.

Approach 1: Using set() + next()

In this, initially, the check container is converted to set, and each element is checked using next() and generator expression. The next() function returns the first element matching, else if no match element is found, None is returned.

Python3




# Python3 code to demonstrate working of
# First occurrence of one list in another
# Using next() + set()
 
# initializing lists
test_list1 = [1, 6, 3, 7, 8, 9, 2]
test_list2 = [4, 10, 8, 2, 0, 11]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# converting test list to sets
test_list2 = set(test_list2)
 
# stops when 1st match element is found
res = next((ele for ele in test_list1 if ele in test_list2), None)
 
# printing result
print("First element in list 1 from 2 : " + str(res))

Output

The original list 1 is : [1, 6, 3, 7, 8, 9, 2]
The original list 2 is : [4, 10, 8, 2, 0, 11]
First element in list 1 from 2 : 8

Approach 2: Using for loops

Python3




# Python3 code to demonstrate working of
# First occurrence of one list in another
 
# initializing lists
test_list1 = [1, 6, 3, 7, 8, 9, 2]
test_list2 = [4, 10, 18, 12, 0, 11]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
 
# stops when 1st match element is found
res=None
for i in test_list2:
    if i in test_list1:
        res=i
        break
 
# printing result
print("First element in list 1 from 2 : " + str(res))

Output

The original list 1 is : [1, 6, 3, 7, 8, 9, 2]
The original list 2 is : [4, 10, 18, 12, 0, 11]
First element in list 1 from 2 : None

Approach 3:  using Counter() function

Python3




# Python3 code to demonstrate working of
# First occurrence of one list in another
from collections import Counter
# initializing lists
test_list1 = [1, 6, 3, 7, 8, 9, 2]
test_list2 = [4, 10, 18, 12, 0, 11]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
freq1= Counter(test_list1)
 
# stops when 1st match element is found
res=None
for i in test_list2:
    if i in freq1.keys():
        res=i
        break
 
# printing result
print("First element in list 1 from 2 : " + str(res))

Output

The original list 1 is : [1, 6, 3, 7, 8, 9, 2]
The original list 2 is : [4, 10, 18, 12, 0, 11]
First element in list 1 from 2 : None

Time Complexity: O(N)

Auxiliary Space : O(N)

Approach 4: Using recursive method.

Python3




# Python3 code to demonstrate working of
# First occurrence of one list in another
#using recursive approach
def first_match(start,lst1,lst2):
  if start==len(lst1):  #base condition
    return None
  if lst1[start] in lst2:   #checking if lst1[start] present in lst2 or not
    return lst1[start]
  return first_match(start+1,lst1,lst2)  #calling recursive call
 
# initializing lists
test_list1 = [1, 6, 3, 7, 8, 9, 2]
test_list2 = [4, 10, 8, 12, 0, 11]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
 
 
res=first_match(0,test_list1,test_list2)
 
# printing result
print("First element in list 1 from 2 : " + str(res))
 
#this code contributed by tvsk

Output

The original list 1 is : [1, 6, 3, 7, 8, 9, 2]
The original list 2 is : [4, 10, 8, 12, 0, 11]
First element in list 1 from 2 : 8

Time Complexity: O(N)

Auxiliary Space : O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!