Python – Test Common Elements Order
Last Updated :
26 Apr, 2023
Sometimes, while working with lists, we can have a problem in which we need to test if list contains common elements. This type of problem has been dealt many times and has lot of solutions. But sometimes, we might have a problem in which we need to check that those common elements appear in same order in both list or not. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + set() The combination of above functions can be used to perform this task. In this, we iterate through the lists and check using conditions of we have in order common elements or not. The duplication removal is performed using set().
Python3
def common_ord(test_list1, test_list2):
comm = set (test_list1)
comm.intersection_update(test_list2)
pr_idx = 0
for ele in test_list1:
if ele in comm:
try :
pr_idx = test_list2.index(ele, pr_idx)
except ValueError:
return False
return True
test_list1 = [ 'Gfg' , 'is' , 'for' , 'Geeks' ]
test_list2 = [ 1 , 'Gfg' , 2 , 'Geeks' ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
res = common_ord(test_list1, test_list2)
print ("Are common elements in order ? : " + str (res))
|
Output :
The original list 1 is : ['Gfg', 'is', 'for', 'Geeks']
The original list 2 is : [1, 'Gfg', 2, 'Geeks']
Are common elements in order ? : True
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #2 : Using list comprehension + set() The combination of above functions perform the task in similar way. The difference is that we use shorter constructs as compared to upper method.
Python3
test_list1 = [ 'Gfg' , 'is' , 'for' , 'Geeks' ]
test_list2 = [ 1 , 'Gfg' , 2 , 'Geeks' ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
temp = set (test_list1) & set (test_list2)
temp1 = [val for val in test_list1 if val in temp]
temp2 = [val for val in test_list2 if val in temp]
res = temp1 = = temp2
print ("Are common elements in order ? : " + str (res))
|
Output :
The original list 1 is : ['Gfg', 'is', 'for', 'Geeks']
The original list 2 is : [1, 'Gfg', 2, 'Geeks']
Are common elements in order ? : True
Method 3 : using the built-in function filter().
steps
Initialize the two input lists.
Convert one of the lists into a set and assign it to a variable.
Define a lambda function that will take an input value and return True if it exists in the set.
Use filter() function with the lambda function and the second list as the input to filter out all elements that do not exist in the set.
Convert the filtered list into a set and check if it’s equal to the set of the common elements.
Print the result.
Python3
test_list1 = [ 'Gfg' , 'is' , 'for' , 'Geeks' ]
test_list2 = [ 1 , 'Gfg' , 2 , 'Geeks' ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
common_elements = set (test_list1) & set (test_list2)
filtered_list = list ( filter ( lambda x: x in common_elements, test_list2))
res = filtered_list = = [val for val in test_list1 if val in common_elements]
print ( "Are common elements in order ? : " + str (res))
|
Output
The original list 1 is : ['Gfg', 'is', 'for', 'Geeks']
The original list 2 is : [1, 'Gfg', 2, 'Geeks']
Are common elements in order ? : True
The time complexity of this approach is O(n), where n is the length of the second list, as we only loop through the second list once using filter().
The auxiliary space complexity of this approach is O(m), where m is the number of common elements between the two lists, as we create a set to store the common elements.
Share your thoughts in the comments
Please Login to comment...