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)) |
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
# 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)) |
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
# 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)) |
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 |
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)) #This code is contributed by Jyothi pinjala. |
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
# 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 , 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)) # using list comprehension try : # Find the first element in test_list2 that is also in test_list1 res = next (i for i in [i for i in test_list2 if i in test_list1]) except StopIteration: # If there are no matching elements, set res to None res = None # printing result print ( "First element in list 1 from 2 : " + str (res)) #This code is contributed by Vinay Pinjala. |
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
# 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 , 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)) # filter all comman element from list. res = [ filter ( lambda a : ( a in test_list2 ), test_list1 )][ 0 ] # printing result print ( "First element in list 1 from 2 : " + str (res)) |
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.