Not the most efficient one, but by far the most obvious way to do it is:

>>> a = [1, 2, 3, 4, 5]
>>> b = [9, 8, 7, 6, 5]
>>> set(a) & set(b)
{5}

if order is significant you can do it with list comprehensions like this:

>>> [i for i, j in zip(a, b) if i == j]
[5]

(only works for equal-sized lists, which order-significance implies).

Answer from SilentGhost on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-difference-two-lists
Difference between Two Lists in Python - GeeksforGeeks
November 27, 2025 - Below are several ways to get the difference between two lists in Python. This method finds the difference by converting both lists into sets and subtracting them. The - operator removes every element from the first set that also exists in the second set, giving only the unique elements from the first list. ... This method finds the difference by comparing how many times each element appears in both lists.
Discussions

how to best compare two lists using a for loop?
You're comparing the entry in a (call it x) with the entry at the xth position in b. If you want to go through things stepwise like this, consider using the enumerate function to keep indices consistent. newlist = [] for i, value in enumerate(a): if a[i] == b[i]: newlist.append(i) print(newlist) Here's how that looks as a list comprehension: print([i for i, val in enumerate(a) if a[i] == b[i]]) More on reddit.com
๐ŸŒ r/learnpython
23
9
August 17, 2022
How does Python compare 2 lists?
In JavaScript, if you try to compare 2 arrays like this: let a = [1, 2, 3] let b = [1, 2, 3] a === b It just doesn t work since they are reference types and you compare the memory adresses. But if you do a comparison like that in Python it works. Does anyone know how Python does this? More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
5
0
February 21, 2020
Compare two lists
I have 2 lists and need to compare them, to print out which lastnames and firstnames are missing for the second list. Note that the order of the lastnames and/or firstnames (separated by comma) elements can be random. The output should be something like: MISSING ENTRIES --------------------- ... More on discuss.python.org
๐ŸŒ discuss.python.org
2
0
December 15, 2023
Finding difference between two lists without removing duplicates
Maybe a list is not the data structure you want. The operation you want to do becomes trivial when using a collections.Counter. Just converting from and to lists is clumsy. from collections import Counter list1 = ['black', 'white', 'white'] list2 = ['black', 'white'] diff = list((Counter(list1) - Counter(list2)).elements()) print(diff) Edit: better example from collections import Counter multiset1 = Counter(['black', 'white', 'white']) multiset2 = Counter(['black', 'white']) diff = multiset1 - multiset2 print(diff) More on reddit.com
๐ŸŒ r/learnpython
14
9
June 2, 2022
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-compare-two-lists-in-python
How to Compare Two Lists in Python | DigitalOcean
July 22, 2025 - For comparing list contents regardless of order, collections.Counter is the most efficient and reliable method as it correctly handles duplicate elements. Leverage Pythonโ€™s set operations (&, -, ^) for a highly efficient way to find common elements, differences, or unique items between two lists.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to best compare two lists using a for loop?
r/learnpython on Reddit: how to best compare two lists using a for loop?
August 17, 2022 -

The task is to take two lists and print the index of every value that matches using a for loop.

a = [0,1,2,3,4,6,4,5,8]
b = [0,5,6,7,4,6,3,5,8]

newlist = []


for number in a: 
    if number == b[number]:
        newlist.append(b.index(number))

print(newlist)

The output should be: 0,4,5,8

The output is: 0,4,4,8

After some research I found a lot of better ways of doing this exercise (list comprehensions etc.) . I can not figure out though what the mistake is in this example. I already tried variations of this using range() - for number in range(len a)

What am I missing here?

Edit: sry - I meant the output should be 0,4,5,7,8

๐ŸŒ
Pybites
pybit.es โ€บ articles โ€บ comparing_lists
Comparing Lists with Difflib โ€“ Pybites
March 8, 2017 - This is where I got stuck. I came up with a for loop that checked to see if items from the text1_split list were in the text2_split list.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ t โ€บ how-does-python-compare-2-lists โ€บ 352127
How does Python compare 2 lists? - The freeCodeCamp Forum
February 21, 2020 - In JavaScript, if you try to compare 2 arrays like this: let a = [1, 2, 3] let b = [1, 2, 3] a === b It just doesn t work since they are reference types and you compare the memory adresses. But if you do a comparison like that in Python it works. Does anyone know how Python does this?
Find elsewhere
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Compare two lists - Python Help - Discussions on Python.org
December 15, 2023 - I have 2 lists and need to compare them, to print out which lastnames and firstnames are missing for the second list. Note that the order of the lastnames and/or firstnames (separated by comma) elements can be random. The output should be something like: MISSING ENTRIES --------------------- lastname: Joseph missing: Matthew lastname: Richard missing: Charles lastname: Michael missing completely The code so far is: import pprint data1 = [ {"lastname": "Joseph", "firstnames": "Thomas, Chr...
๐ŸŒ
Centron
centron.de โ€บ startseite โ€บ how to compare two lists in python
Compare Two Lists in Python: A Comprehensive Guide
February 4, 2025 - Learn how to compare two lists in Python with methods like sort(), set(), and reduce() using simple examples.
๐ŸŒ
Miguendes
miguendes.me โ€บ python-compare-lists
The Best Ways to Compare Two Lists in Python
July 2, 2022 - Check if two lists are equal, which elements match, get the difference between two lists, compare lists of dictionaries, list of strings and more! UpdatedJuly 2, 2022โ€ข17 min readโ€ขView as Markdown ... AI Software Engineer based in London, UK with 6+ years of professional experience developing and releasing software in different programming languages - Hobbyist Technical Writer - Interested in Software Testing, Best Practices, Scalability, Machine Learning/AI, and Python.
๐ŸŒ
LARG*net
largnet.ca โ€บ largblog โ€บ pythoncomparelists
Python compare two lists or more. โ€” LARG*net - London and Region Global Network
November 2, 2022 - The index increases when list1 has been checked against all other lists so it moves on to compare list2 and list3. Thereโ€™s also no need for list 3 to go back since it has already been checked against all other lists. The list of feeds can be as long as you please and you end up with any overlaps that exist. This first step of analysis tells us that about 5,000 out of >100,000 possible entries are on more than 1 list.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-compare-two-lists-of-letters-without-using-built-in-functions-in-Python-I-want-to-iterate-thought-both-the-lists-to-check-if-each-element-of-List-1-is-present-in-List-2-without-using-for-x-in-a-list
How do I compare two lists of letters without using built-in functions in Python? I want to iterate thought both the lists to check if ea...
Answer (1 of 4): Hi, I am not sure why you would want to do this without using in built functions, however a list comprehension is probably what you want. Example list1 = [โ€˜aโ€™, โ€˜cโ€™, โ€˜eโ€™, โ€˜fโ€™] list2 = [โ€˜bโ€™, โ€˜dโ€™, โ€˜cโ€™, โ€˜gโ€™] y = [x for x in list1 if x in list2] y ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ sorting.html
Sorting Techniques โ€” Python 3.14.6 documentation
So for example the original list could contain complex numbers which cannot be sorted directly. Another name for this idiom is Schwartzian transform, after Randal L. Schwartz, who popularized it among Perl programmers. Now that Python sorting provides key-functions, this technique is not often needed. Unlike key functions that return an absolute value for sorting, a comparison function computes the relative ordering for two inputs.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-check-if-two-lists-are-identical
Check if two lists are identical in Python - GeeksforGeeks
July 11, 2025 - The easiest way to check if two lists are identical is by using the equality operator (==). This method compares each element of both lists in the same order and return True if they match, otherwise False.
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ compare-list-elements-python
How to Compare Two Elements of a List in Python (2026)
October 17, 2024 - Python gives you a clear ladder of tools for comparing list elements. Use index comparison for one known pair, loops with zip or itertools.pairwise for adjacent items, comprehensions to collect results in one line, and built-ins with sets for duplicates, min, max, and overlap. Mind the two classic traps, == versus is and exact float comparison, and guard short lists against index errors.
๐ŸŒ
CompareTwoLists.com
comparetwolists.com
Compare two lists - easy online listdiff tool
Want to compare lists of Instagram followers, names, e-mails, domains, genes or something else? This tool shows you the unique and shared values in your two lists.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ list โ€บ determine-if-two-lists-have-same-elements
Determine if Two Lists Have Same Elements, Regardless of Order - AskPython
March 31, 2023 - We can use the equal-to (โ€˜==โ€™) operator to compare the two sorted lists. We can infer that the original lists have the same elements but at different locations, if the sorted lists have the same entries in the same indices. To learn about operators in Python, check out this article.
๐ŸŒ
Shapehost
shape.host โ€บ home โ€บ resources โ€บ comparing lists in python: a comprehensive guide
Comparing Lists in Python: A Comprehensive Guide - Shapehost
December 1, 2023 - Using the reduce() and map() functions is a powerful method to compare lists, especially when you need to perform element-wise comparison. However, itโ€™s important to note that the order of the list items is crucial when using these functions. Python provides the set() function, which can ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python list comparison
Python List Comparison - Scaler Topics
May 4, 2023 - In this blog, we will explore numerous approaches to compare two lists. The l1 == l2 operator for element-wise comparison is the most Pythonic approach to verify if two ordered lists l1 and l2 are identical. The return result is True if all entries are equal and the lengths of the lists are ...
๐ŸŒ
PythonForBeginners.com
pythonforbeginners.com โ€บ home โ€บ compare two lists in python
Compare two lists in Python - PythonForBeginners.com
October 22, 2021 - To compare two lists in python, we can use sets. A set in python only allows unique values in it.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ comparing-python-lists-without-order
Comparing Python Lists Without Order - GeeksforGeeks
July 23, 2025 - Explanation: The sorted() function returns a new list with the elements arranged in ascending order. If the sorted versions of both lists are the same, it means they contain the same elements. If we donโ€™t care about element frequency (i.e., duplicates are irrelevant), we can use Pythonโ€™s set to compare lists.