Open In App

Python | Find common elements in list of lists

The problem of finding the common elements in list of 2 lists is quite a common problem and can be dealt with ease and also has been discussed before many times. But sometimes, we require to find the elements that are in common from N lists. Let’s discuss certain ways in which this operation can be performed. 

Method #1 : Using reduce() + lambda + set() This particular task can be achieved in just a one line using the combination of the above functions. The reduce function can be used to operate the function of “&” operation to all the list. The set function can be used to convert list into a set to remove repetition. 






# Python3 code to demonstrate
# common element extraction form N lists
# using reduce() + lambda + set()
from functools import reduce
 
# initializing list of lists
test_list = [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# common element extraction form N lists
# using reduce() + lambda + set()
res = list(reduce(lambda i, j: i & j, (set(x) for x in test_list)))
 
# printing result
print ("The common elements from N lists : " + str(res))

Output:
The original list is : [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
The common elements from N lists : [2, 3]

Time complexity: O(n*m*log(m)), where n is the number of lists and m is the maximum length of a list. 
Auxiliary space: O(m), where m is the maximum length of a list.



Method #2: Using map() + intersection() The map function can be used to convert each of the lists to set to be operated by to perform the intersection, using the set.intersection function. This is the most elegant way to perform this particular task. 




# Python3 code to demonstrate
# common element extraction form N lists
# using map() + intersection()
 
# initializing list of lists
test_list = [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# common element extraction form N lists
# using map() + intersection()
res = list(set.intersection(*map(set, test_list)))
 
# printing result
print ("The common elements from N lists : " + str(res))

Output:
The original list is : [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
The common elements from N lists : [2, 3]

Time complexity: O(N*M), where N is the number of lists and M is the average length of each list.
Auxiliary space: O(M), where M is the maximum length of any list

Method #3 : Using itertools

Here is another approach that could be used to find the common elements in a list of lists. This approach involves using the itertools module’s product function to create a list of all possible pairs of elements from the lists, and then using a list comprehension to filter this list down to only the pairs where the elements are equal. This approach has a time complexity of O(n^2) and a space complexity of O(n^2), as it requires generating a list of all possible pairs of elements and then filtering this list down to only the common elements.

Here is an example of how this approach could be implemented in Python:




# Python3 code to find the common elements in a list of lists
 
# Initialize the list of lists
test_list = [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
 
# Print the original list
print("The original list is:", test_list)
 
# Find the common elements
import itertools
pairs = list(itertools.product(*test_list))
common_elements = set([x[0] for x in pairs if x[0] == x[1] and x[0] == x[2]])
 
# Print the result
print("The common elements are:", common_elements)
#This code is contributed by Edula Vinay Kumar Reddy

Output
The original list is: [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
The common elements are: {2, 3}

The time complexity of the above code to find the common elements in a list of lists is O(n^2), where n is the total number of elements in all the sublists combined.

The space complexity of the code is O(n), where n is the total number of elements in all the sublists combined. 

Method #4: Using list comprehension and set intersection

Use list comprehension and set intersection to find the common elements from N lists.




# Python3 code to demonstrate
# common element extraction form N lists
# using list comprehension and set intersection
 
# initializing list of lists
test_list = [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# common element extraction form N lists
# using list comprehension and set intersection
res = list(set(test_list[0]).intersection(*test_list[1:]))
 
# printing result
print("The common elements from N lists : " + str(res))

Output
The original list is : [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
The common elements from N lists : [2, 3]

Time Complexity: O(n * k) where n is the number of lists and k is the maximum length of the lists. 
Auxiliary Space: O(k) where k is the maximum length of the lists.

Method #5: Using a loop to compare each element in the first list with the other lists

This method iterates through each element in the first list and checks if it exists in all the other lists using a loop and the all() function. If an element exists in all the other lists, it is added to the common_elements list.




test_list = [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
common_elements = []
 
for element in test_list[0]:
    if all(element in sublist for sublist in test_list[1:]):
        common_elements.append(element)
 
print("The common elements from N lists : " + str(common_elements))

Output
The common elements from N lists : [2, 3]

The time complexity of the above code is O(n*m^2), where n is the length of the first sublist and m is the number of sublists in the test_list.

The auxiliary space complexity of the code is O(k), where k is the number of common elements in all the sublists. 


Article Tags :