Python Program to Find Intersection of Two Lists

The intersection of two lists is elements that appear on both lists. There are multiple ways to obtain the intersection of two lists using Python, like using loops, set operations, and list comprehensions.

Methods to Get Intersection of Two Lists

1. Using Loop

This uses a loop in order to compare each element in one list to the other list.

2. Using List Comprehension

A more elegant way of expressing the same as above using list comprehension in Python.

3. Using set.intersection() Method

Using Python’s set data type to find intersection in an efficient manner.

4. Using filter() and lambda

Functional Programming using filter() and lambda.

Method 1: Loop

  1. Create two lists.
  2. Initialize an empty list to store the intersection.
  3. Iterate through the first list.
  4. If an element exists in the second list and is not already in the result, add it.
  5. Print the final result.
# Define two lists
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]

# Empty list to store intersection
intersection = []

# Loop through the first list
for item in list1:
    if item in list2 and item not in intersection:
        intersection.append(item)

# Print the intersection
print("Intersection:", intersection)

Output:

Intersection: [4, 5, 6]

Time Complexity

  • O(n * m) (where n and m are lengths of the two lists) because for each element in list1, we check if it’s in list2.

Method 2: Using List Comprehension

  • This method is similar to the loop method but more concise.
  • We use list comprehension to filter common elements.
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]

# List comprehension to find intersection
intersection = [item for item in list1 if item in list2]

print("Intersection:", intersection)

Output:

Intersection: [4, 5, 6]

Time Complexity

  • O(n * m), since for each item in list1, it checks if it exists in list2.

Method 3: Using set.intersection()

  • Convert both lists to sets.
  • Use the .intersection() method to find common elements.
  • Convert the result back to a list.
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]

# Convert to sets and find intersection
intersection = list(set(list1).intersection(set(list2)))

print("Intersection:", intersection)

Output:

Intersection: [4, 5, 6]

Time Complexity

  • O(n + m) (faster than previous methods) because:
    • Converting lists to sets takes O(n + m).
    • Finding the intersection takes O(min(n, m)).

Method 4: Using filter() and lambda

  • The filter() function filters elements based on a condition.
  • The lambda function checks if an element from list1 is in list2.
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]

# Using filter() and lambda
intersection = list(filter(lambda x: x in list2, list1))

print("Intersection:", intersection)

Output:

Intersection: [4, 5, 6]

Time Complexity

  • O(n * m) because lambda x: x in list2 checks for each element in list1 whether it exists in list2.

Best Method?

MethodCode SimplicityPerformance
LoopEasySlow (O(n*m))
List ComprehensionConciseSlow (O(n*m))
set.intersection()BestFastest (O(n+m))
filter() + lambdaFunctional StyleSlow (O(n*m))

Conclusion

  • For large lists, Method 3 (Set Intersection) is the best as it has the fastest performance (O(n + m)).
  • If lists are small, any method works fine.