How to Find Intersection in a Python List

Python list intersection is one of the more advanced techniques within the language. We don’t have any built-in functions or methods to accomplish that task. However, by achieving that goal we can truly experience Python’s power and flexibility. Even without any built-in functionality, we can typically find intersections without needing to write much code. Consider the following example.

def findIntersection(tmpList1, tmpList2):
    tempList3 = [value for value in tmpList1 if value in tmpList2]
    return tempList3
ourList1 = [8, 1, 2, 15, 77, 42, 99, 5, 3]
ourList2 = [3, 1, 5, 7, 88, 12, 42, 22, 29]
print(findIntersection(ourList1, ourList2))

We begin by creating a findIntersection function which takes two lists as parameters. These are the two lists we’ll look through in order to find shared unique values. In order to do so we’ll use list comprehension in the creation of our tempList3 variable. The logic creates a loop that checks if a given value from tempList1 is in tmpList2. If so than it’s included in the tempList3 variable. Finally, the function returns tempList3.

We proceed to actually declare and populate the ourList1 and ourList2 lists. Finally, we end the function by actually calling the findIntersection function that we created. We call it using the ourList1 and ourList2 lists as parameters. The Python intersection method function is called from within print so that we can see the resulting intersection. One of the most impressive things about this is that the actual logic only takes up two lines of code. The rest is just function definition and call or variable declaration.

This is far from the only way to find intersections in a list or nested list with a Python program. We can also take advantage of another sequence container called sets. The following example will take a different approach but still provides us with the same intersection points.

def findIntersection(tmpList1, tmpList2):
    return list(set(tmpList1) & set(tmpList2))
ourList1 = [8, 1, 2, 15, 77, 42, 99, 5, 3]
ourList2 = [3, 1, 5, 7, 88, 12, 42, 22, 29]
print(findIntersection(ourList1, ourList2))

We begin by creating a new findIntersection function. As with the previous example, it takes two lists as parameters. And like that Python intersection operation example it also returns a list. But this function works by creating a new set from the nested list values. An important point of difference between sets and lists is that sets can’t contain duplicate values. We essentially do a quick conversion from list to set and then back again.

We then proceed to define our two lists and call findIntersection within a print call. From that point we can see that while using a different method thanks to the set object we’ve been able to get the same result. But the Python set intersecion is just another approach to this end goal. There’s still other methods we can use to reach that point. In fact, we can make it even more concise. We can essentially boil it down to a single line of code in the next example.

ourList1 = [8, 1, 2, 15, 77, 42, 99, 5, 3]
ourList2 = [3, 1, 5, 7, 88, 12, 42, 22, 29]
intersectPoints = [x for x in ourList2 if x in ourList1]
print(intersectPoints)

We define two lists with the same values that we’ve used up to this point. We then create a new list, intersectPoints. Our intersection operation will actually take place within this definition. This Python code looks for a common element through similar list comprehension to what we used in the first example. However, this time around we’re looking for the proper subset by comparing linked lists directly within the declaration of a third list of common values. In comparison, we used a new input function to accomplish this in the first example. Finally, we end by displaying the intersection as output to the screen. Note how concise this Python program example is.

Other than calling print and creating lists there’s only really one line of code. And even that is logic contained within the declaration of a new set or second list. This method is more concise than the first input example. But the drawback comes from needing to perform this same trick multiple times if we need to use it again, though it works great for unique elements in a given set it may be difficult when there are duplicates in the original set or Python list. Whereas in the first example the list comprehension is leveraged within a function that’s easy to reuse. Neither is inherently better or worse. They just have different strengths depending on what we need to use them for.

How to Find Intersection in a Python List

Leave a Reply

Your email address will not be published.

Scroll to top