Python – First occurrence of one list in another
Last Updated :
05 May, 2023
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
test_list1 = [ 1 , 6 , 3 , 7 , 8 , 9 , 2 ]
test_list2 = [ 4 , 10 , 8 , 2 , 0 , 11 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
test_list2 = set (test_list2)
res = next ((ele for ele in test_list1 if ele in test_list2), None )
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
Time complexity: O(n), where n is the length of the test_list. The set() + next() takes O(n) time
Auxiliary Space: O(1), extra space required is not required
Approach 2: Using for loops
Python3
test_list1 = [ 1 , 6 , 3 , 7 , 8 , 9 , 2 ]
test_list2 = [ 4 , 10 , 18 , 12 , 0 , 11 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = None
for i in test_list2:
if i in test_list1:
res = i
break
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 3: using Counter() function
Python3
from collections import Counter
test_list1 = [ 1 , 6 , 3 , 7 , 8 , 9 , 2 ]
test_list2 = [ 4 , 10 , 18 , 12 , 0 , 11 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
freq1 = Counter(test_list1)
res = None
for i in test_list2:
if i in freq1.keys():
res = i
break
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
def first_match(start,lst1,lst2):
if start = = len (lst1):
return None
if lst1[start] in lst2:
return lst1[start]
return first_match(start + 1 ,lst1,lst2)
test_list1 = [ 1 , 6 , 3 , 7 , 8 , 9 , 2 ]
test_list2 = [ 4 , 10 , 8 , 12 , 0 , 11 ]
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)
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, 12, 0, 11]
First element in list 1 from 2 : 8
Time Complexity: O(N)
Auxiliary Space : O(N)
Approach 5: Using intersection method.
Python3
test_list1 = [ 1 , 6 , 3 , 7 , 8 , 9 , 2 ]
test_list2 = [ 4 , 10 , 8 , 2 , 0 , 11 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
result = set (test_list1).intersection(test_list2)
if result:
res = result.pop()
else :
res = None
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
Time Complexity: O(N)
Auxiliary Space : O(N)
Approach 6: Using list comprehension
Python3
test_list1 = [ 1 , 6 , 3 , 7 , 8 , 9 , 2 ]
test_list2 = [ 4 , 10 , 8 , 2 , 0 , 11 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
try :
res = next (i for i in [i for i in test_list2 if i in test_list1])
except StopIteration:
res = None
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
Time Complexity: O(N)
Auxiliary Space : O(N)
Approach 7: Using filter method
Algorithm:
- InitaInitializelize list1.
- Initialize list2.
- Use the filter method on list1 that filter out element of list1 present in list2.
- Print the first element.
Python
test_list1 = [ 1 , 6 , 3 , 7 , 8 , 9 , 2 ]
test_list2 = [ 4 , 10 , 8 , 12 , 0 , 11 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = [ filter ( lambda a : ( a in test_list2 ), test_list1 )][ 0 ]
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, 12, 0, 11]
First element in list 1 from 2 : [8]
Time Complexity: O(N) where N is the length of list1.
Auxiliary Space: O(M) Where M is the length of the new filter list.
Share your thoughts in the comments
Please Login to comment...