If order is not important and you don't need to worry about duplicates then you can use set intersection:

>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) & set(b))
[1, 3, 5]
Answer from Mark Byers on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-intersection-two-lists
Intersection of Two Lists - Python - GeeksforGeeks
November 18, 2025 - Explanation: .intersection() returns only the elements that exist in both sets. This method counts how many times each value appears in both lists and keeps only the elements with matching counts.
🌐
Reddit
reddit.com › r/learnpython › how to find intersections between lists? (novice)
r/learnpython on Reddit: How to find intersections between lists? (novice)
August 7, 2021 -

Hi all,

I've got an assignment I'm working on for a bioinformatics course in which we've converted several text files into lists, and want to find each instance of any two of these lists having an item in common.

For example, let's say we've got Jenny, Martha, Bertha, and Ingrid. Each of these fine ladies has a grocery list, and what we want to do is compare each of these ladies' grocery lists with one another and return any items that are in common. As such...

martha_list = ['Eggs','Sugar','Milk']
bertha_list = ['Bread','Cereal','Popcorn']
ingrid_list = ['Flour','Milk']
jenny_list = ['Flour','Milk','Poptarts']

What I want to do is establish a for loop that compares every possible combination of grocery lists, and returns each example of two lists sharing a common item. Here's what I have:

groceries = [martha_list, bertha_list, ingrid_list, jenny_list]

for i in range(len(groceries)):
for j in range(i+1,len(groceries)):

comp = (groceries[i] & groceries[j])

## or: comp = groceries[i].intersection(groceries[j])

print(comp,"\n")

Both the "intersection" and "&" strategies return different errors: the former an AttributeError ('list' object has no attribute 'intersection'), and the latter a TypeError (unsupported operand type(s) for &: 'list' and 'list').

I tried to fix this by converting the list objects into sets, but a set(groceries) or set(groceries[i]) command returns another TypeError ('list' object is not callable).

I've tried googling the hell out of this and haven't been able to find a solution, so any help would be very appreciated. I know that my print command at the end won't return exactly what I want, but I'm having a hard time coming up with what I'll want to print without knowing how to set the loop up to begin with.

🌐
W3Schools
w3schools.com › python › ref_set_intersection.asp
Python Set intersection() Method
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training · ❮ Set Methods · Return a set that contains the items that exist in both set x, and set y: x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.intersection(y) print(z) Try it Yourself » ·
🌐
Flexiple
flexiple.com › python › intersection-two-list-python
Intersection of Two Lists in Python - Flexiple
April 2, 2024 - In Python, the intersection of two lists is a crucial operation when working with data. It helps us identify common elements shared between two lists.
🌐
HackerNoon
hackernoon.com › list-comparison-in-python-differences-intersections-and-unions
List Comparison in Python: Differences, Intersections, and Unions | HackerNoon
March 8, 2024 - Check out how to compare lists like a Python pro with differences, intersections, unions, and more, featuring easy-to-follow examples.
Find elsewhere
🌐
Educative
educative.io › answers › how-to-find-intersection-of-two-lists-in-python
How to find intersection of two lists in Python
Using the intersection() method: Python sets have a built-in intersection() method that can be used to find the common elements between two sets. We can use this method to find the intersection of two lists by converting ...
🌐
Reddit
reddit.com › r/learnpython › intersection of 2 lists without using the in operator or any built-in functions
r/learnpython on Reddit: intersection of 2 lists without using the in operator or any built-in functions
April 13, 2022 -

Does anyone know how i can use a merging algorithm that is efficient and reduces the number of comparison to a minimum? (a and b are sorted lists)
I thought about an skip pointer but i don't know how to implement it

dict_ratings = {}

a = sorted([f for f in inverted_index['american']])
b = sorted([f for f in inverted_index['dream']])

i, j = 0, 0
intersection = []
while i < len(a) and j < len(b):
    if a[i] == b[j]:
        intersection.append(a[i])
        i += 1
        j += 1
    elif a[i] > b[j]:
        j += 1
    else:
        i += 1
        
print(intersection)
Top answer
1 of 2
2
If by intersection you mean in both, then you can simply use set-wise operations. If you want the result to be sorted, you can just apply the sorted function to it after, although I'm not sure if that breaks your builtins rule. You can also specify a key function if needed. This is assuming you don't have any/don't care about repeating elements. a = set(inverted_index["american"]) b = set(inverted_index["dream"]) intersection = sorted(a & b) print(intersection) You can also use a Counter if you want to keep track of counts as well. It usually takes the minimum count iirc. Edit: I think Python's dict has a key view, which can be used like an ordered set, with setwise operations working on them too, so you could do something like dict.fromkeys and then use setwise operations on that.
2 of 2
1
AFAIK, your current algorithm is essentially optimal. The only change that you might have to make is if one of the lists has a character that repeats multiple times. E.g., should the "a" in "american" be included twice, even though there's only one "a" in "dream"? If so, then your algorithm is slightly wrong, although it's a pretty easy fix. Depending on how you expect the first and second list to behave (will there be a significant difference in a[0] and b[0]? will both lists be very large?), it also might be worth it to perform binary search to find a good place to start in the list with lower values. That's a pretty small improvement, though, and will only apply for fairly large data sets.
🌐
Python.org
discuss.python.org › python help
intersection of 2 lists without using the in operator or any built-in functions - Python Help - Discussions on Python.org
April 13, 2022 - Does anyone know how i can use a merging algorithm that is efficient and reduces the number of comparison to a minimum? I thought about an skip pointer but i don’t know how to implement it my code right now: a = [18, 23, 53, 69, 77, 91, 102, 116, 135, 159, 160, 161, 162, 163, 164, 165, 166] b = [7, 120, 138, 159, 160, 161, 162, 163, 164, 165, 166] i, j = 0, 0 intersection = [] while i
🌐
Towards Data Science
towardsdatascience.com › home › latest › finding the intersection of python sets: a practical use case
Finding the Intersection of Python Sets: A Practical Use Case | Towards Data Science
March 5, 2025 - Note that when we say it is ... intersection of two or more sets, which can be easily done using Python’s set.intersection() method....
🌐
Code.org
code.org › en-US › hour-of-code
Hour of Code
Explore the intersection of coding and language arts by creating interactive poems, illustrating the mood and tone with code.
🌐
GitHub
github.com › ltdrdata › ComfyUI-Impact-Pack
GitHub - ltdrdata/ComfyUI-Impact-Pack: Custom nodes pack for ComfyUI This custom node helps to conveniently enhance images through Detector, Detailer, Upscaler, Pipe, and more. · GitHub
April 27, 2026 - SEGS Filter (intersection) - This node filters segs1, keeping only the SEGS that do not significantly overlap with any SEGS in segs2, based on the Intersection over Area (IoA) threshold.
Starred by 3.2K users
Forked by 378 users
Languages   Python 77.3% | JavaScript 11.4% | Shell 10.7% | Jupyter Notebook 0.6%
🌐
Substack
bookdna.substack.com › p › spring-2026-my-to-read-list-curated
Spring 2026: My To-Read List (Curated) - by Lauren Vargas
March 31, 2026 - The collision of climate science and mythological resonance is precisely the kind of intersection that makes my reading life feel alive. Andrew Guthrie Ferguson’s Your Data Will Be Used Against You: Policing in the Age of Self-Surveillance (C/NF) was an immediate addition to my reading list after encountering an article on the body’s betrayal of privacy.
🌐
Uber
uber.com › global › en › careers › list › 157432
Software Engineer II - Earner (multiple teams hiring) - Sunnyvale, California | Uber Careers
We develop the backend platforms responsible for delivering real-time trips, incentives, and promotions tailored to earner preferences and marketplace conditions. Working at the intersection of Data Science and Marketplace teams, we focus on creating reliable infrastructure that improves transparency and drives product innovation.
🌐
Benoit
benoit.paris › posts › jax-ray-marcher
JAX's true calling: Ray-Marching renderers on WebGL - benoit.paris
April 1, 2025 - They’re composable. Want the union of two objects? You take the minimum of their SDF. Want the intersection? max() it is. Here is a non-exhaustive list of what you can do. Here, we’ll be using smooth version of a union, as we want to preserve differentiability.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-intersection-in-tuple-records-data
Python | Intersection in Tuple Records Data - GeeksforGeeks
April 17, 2023 - In this, we just iterate for single list and check if any element occurs in other one. ... # Python3 code to demonstrate working of # Intersection in Tuple Records Data # Using list comprehension # Initializing lists test_list1 = [('gfg', 1), ('is', 2), ('best', 3)] test_list2 = [('i', 3), ('love', 4), ('gfg', 1)] # printing original lists print(&quot;The original list 1 is : &quot; + str(test_list1)) print(&quot;The original list 2 is : &quot; + str(test_list2)) # Intersection in Tuple Records Data # Using list comprehension res = [ele1 for ele1 in test_list1 for ele2 in test_list2 if ele1 == ele2] # printing result print(&quot;The Intersection of data records is : &quot; + str(res))
🌐
TutorialsPoint
tutorialspoint.com › article › python-intersection-of-multiple-lists
Python - Intersection of multiple lists
March 25, 2026 - # initializing the lists list_1 = [[1, 2], [3, 4], [5, 6]] list_2 = [[3, 4], [7, 8]] # finding the common items from both lists result = [sub_list for sub_list in list_1 if sub_list in list_2] # printing the result print("Intersection using list comprehension:", result)
🌐
Zod
zod.dev
Intro | Zod
Zod has a thriving ecosystem of libraries, tools, and integrations. Refer to the Ecosystem page for a complete list of libraries that support Zod or are built on top of it.
🌐
raylib
raylib.com › cheatsheet › cheatsheet.html
raylib - cheatsheet
raylib is a simple and easy-to-use library to enjoy videogames programming. Don't miss latest functions added to raylib... check raylib cheatsheet