This post will discuss how to find common elements from two lists in Python. Show 1. Using intersection() functionA simple and fairly efficient solution is to convert the first list into a set and then call the intersection() function. It returns a new set with elements common to the set with another iterable.
Download Run Code 2. Using bitwise comparisonYou can also use the operator-based counterpart of intersection(), which happens to be bitwise, and (&) operator. But this requires both arguments to be set, as shown below:
Download Run Code 3. Using List ComprehensionFinally, you can find common elements from two lists using list comprehensions by searching each element of the first list in the second list. The time complexity of this solution is quadratic, and this is definitely not a recommended solution for large lists.
Download Run Code That’s all about finding the common elements from two lists in Python.
Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding 🙂
PythonServer Side ProgrammingProgramming It is possible to have a list whose inner elements are also lists. In such cases we may come across a need when we have to find out the common elements among these inner lists. In this article we will find out the approaches to achieve this. With map and intersectionIntersection is a simple mathematical concept of finding the common elements between different sets. Python has the set method which returns a set that contains the similarity between two or more sets. So we first convert the elements of the list into set through a map function and then apply the set method to all this converted lists. ExampleLive Demo listA = [['Mon', 3, 'Tue', 7,'Wed',4],['Thu', 5,'Fri',11,'Tue', 7],['Wed', 9, 'Tue', 7,'Wed',6]] # Given list print("Given list of lists : \n",listA) # Applying intersection res = list(set.intersection(*map(set, listA))) # Result print("The common elements among inners lists : ",res)OutputRunning the above code gives us the following result − Given list of lists : [['Mon', 3, 'Tue', 7, 'Wed', 4], ['Thu', 5, 'Fri', 11, 'Tue', 7], ['Wed', 9, 'Tue', 7, 'Wed', 6]] The common elements among inners lists : ['Tue', 7]With reduce and lambdaWe can also apply the reduce function in python. This function is used to apply a given function passed onto it as argument to all of the list elements mentioned in the sequence passed along. The lambda function finds out the common elements by iterating through each nested list after set is applied to them . ExampleLive Demo from functools import reduce listA = [['Mon', 3, 'Tue', 7,'Wed',4],['Thu', 5,'Fri',11,'Tue', 7],['Wed', 9, 'Tue', 7,'Wed',6]] # Given list print("Given list of lists : \n",listA) # Applying reduce res = list(reduce(lambda i, j: i & j, (set(n) for n in listA))) # Result print("The common elements among inners lists : ",res)OutputRunning the above code gives us the following result − Given list of lists : [['Mon', 3, 'Tue', 7, 'Wed', 4], ['Thu', 5, 'Fri', 11, 'Tue', 7], ['Wed', 9, 'Tue', 7, 'Wed', 6]] The common elements among inners lists : ['Tue', 7]
Published on 13-May-2020 14:53:11 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()
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]
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] |