Open In App

Python – Test Common Elements Order

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# Test Common Elements Order
# using loop + set()
 
# helper function
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
 
# Initializing lists
test_list1 = ['Gfg', 'is', 'for', 'Geeks']
test_list2 = [1, 'Gfg', 2, 'Geeks']
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Test Common Elements Order
# using loop + set()
res = common_ord(test_list1, test_list2)
 
# printing result
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




# Python3 code to demonstrate
# Test Common Elements Order
# using list comprehension + set()
 
# Initializing lists
test_list1 = ['Gfg', 'is', 'for', 'Geeks']
test_list2 = [1, 'Gfg', 2, 'Geeks']
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Test Common Elements Order
# using list comprehension + set()
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
 
# printing result
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




# Python3 code to demonstrate
# Test Common Elements Order
# using filter() function
 
# Initializing lists
test_list1 = ['Gfg', 'is', 'for', 'Geeks']
test_list2 = [1, 'Gfg', 2, 'Geeks']
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Test Common Elements Order
# using filter() function
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]
 
# printing result
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads