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
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-compare-two-lists-in-python
How to Compare Two Lists in Python | DigitalOcean
July 22, 2025 - When your goal is to find which elements are shared or which are unique between two lists, the most efficient and Pythonic approach is to use the set() function. It creates set objects using the given lists and then compares the sets for equality using the == operator.
Discussions

How can I compare two lists in python and return not matches - Stack Overflow
And if you don't care that the result should be a list you could just skip the final casting. ... This will discard duplicates in a or b that are not matched by the other. 2016-03-01T01:36:14.933Z+00:00 ... It will, but comparing matches with duplicates makes little sense. 2016-03-01T01:36:58.66Z+00:00 ... Unless it has sense... I don't think you can assert that it doesn't have a purpose without knowing the application. 2016-03-01T01:37:52.173Z+00:00 ... "I would like to return ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python: compare two lists and return matches in order - Stack Overflow
I have two lists of unequal length and I would like to compare and pull matched values in the order from the first list, so a in this example. a = ['a','s','d','f'] b = ['e','d','y','a','t','v'] More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 28, 2014
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 can I compare two lists and return the positions(?) of not matches
You could do a if statement in a for loop Ex : for I in range (5): If l1[i] != l2[i] : print(i) It would output 1 and 4 since index starts at 0 If for some whatever reason you want 2 and 5 you can do print(I + 1) instead More on reddit.com
๐ŸŒ r/PythonLearning
7
1
December 19, 2024
๐ŸŒ
Statistics Globe
statisticsglobe.com โ€บ home โ€บ python programming language for statistics & data science โ€บ compare two lists & return matches & differences in python (2 examples)
How to Compare Two Python Lists & Return Matches & Differences
July 7, 2023 - Hereโ€™s an example: # Finding matches between lists matches = [element for element in list1 if element in list2] print(matches) # [4, 5] In this example, we use a list comprehension to iterate over the elements of list1 and check if each element ...
๐ŸŒ
Squash
squash.io โ€บ how-to-compare-two-lists-in-python-and-return-matches
How to Compare Two Lists in Python and Return Matches
November 2, 2023 - One way to compare two lists in Python and return the matches is by using list comprehension.
๐ŸŒ
Miguendes
miguendes.me โ€บ python-compare-lists
The Best Ways to Compare Two Lists in Python
July 2, 2022 - Learn to compare if two lists are equal, find elements that match, get the difference between 2 lists, compare lists of dictionaries, and list of strings.
๐ŸŒ
W3docs
w3docs.com โ€บ python
How can I compare two lists in python and return matches
Note that the intersection() method returns a set, so we are converting it to a list to be able to print it. Also you can use & operator for intersection ยท list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] matches = list(set(list1) & set(list2)) ...
Find elsewhere
๐ŸŒ
The Web Dev
thewebdev.info โ€บ home โ€บ how to compare two lists and return matches with python?
How to compare two lists and return matches with Python? - The Web Dev
November 20, 2021 - To compare two lists and return matches with Python, we can use the setโ€™s intersection method.
๐ŸŒ
Python Guides
pythonguides.com โ€บ how-to-compare-two-lists-in-python-and-return-non-matches-elements
Compare Two Lists in Python and Get Non-Matching Elements
September 8, 2025 - Learn how to compare two lists in Python and return non-matching elements using simple methods like loops, sets, and list comprehensions with clear examples.
๐ŸŒ
CSEstack
csestack.org โ€บ home โ€บ [6 ways] compare two lists in python and return non-match elements
[6 Ways] Compare Two Lists in Python and Return Non-Match Elements
July 25, 2021 - How to compare two lists in Python? Using lambda function, sort(), set() method to check if two lists are equal and have same elements.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-compare-two-lists-in-python
How to compare two lists in Python?
import collections def ... Same frequency: Equal Different frequency: Not equal ยท This method compares elements at corresponding positions and counts matches....
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Compare Lists in Python | note.nkmk.me
May 10, 2023 - Converting a list to a set (set) allows set operations. To check if two lists partially match, i.e., if they have at least one common element, use the isdisjoint() method of set. It accepts not only sets but also other iterable objects, such ...
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ how-can-i-compare-two-lists-in-python-and-return-matches
How to Compare Two Lists in Python and Return Matches (with Examples) โ€” pythontutorials.net
A for loop is the most intuitive way to compare two lists, especially for beginners. It involves iterating over one list and checking if each element exists in the second list. list1 = [1, 2, 3, 4, 5, 3] # Contains duplicate "3" list2 = [4, ...
๐ŸŒ
STechies
stechies.com โ€บ compare-lists-python-using-set-cmp-function
How to Compare Two Lists in Python using set(), cmp() and difference() Functions
# Custom python code to check if list one is equal to list two by taking difference # Define function name difference def difference (list1, list2): list_dif = [i for i in list1 + list2 if i not in list1 or i not in list2] return list_dif # Initializing list 1 and list 2 x = [10, 15, 20, 25, 30, 35, 40] y = [25, 40, 35] print ("List first: " + str(x)) print ("List second: " + str(y)) # Take difference of list 1 and list 2 z = difference (x, y) print("Difference of first and second String: " + str(z)) # if lists are equal if not z: print("First and Second list are Equal") # if lsts are not equal else: print("First and Second list are Not Equal")
๐ŸŒ
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

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-difference-two-lists
Difference between Two Lists in Python - GeeksforGeeks
November 27, 2025 - Counter subtracts the frequencies, removing elements that appear in both lists and keeping the extra occurrences from the first list. ... from collections import Counter a = [1, 2, 3, 3, 4] b = [3, 4] c = list((Counter(a) - Counter(b)).elements()) print(c) ... Counter(a) creates a frequency map of list a. Counter(a) - Counter(b) subtracts occurrences of matching items. .elements() expands remaining counts back into a list.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-do-a-comparison-of-two-lists-in-Python-with-each-value
How to do a comparison of two lists in Python with each value - Quora
Answer (1 of 8): Depends on what you are trying to do, but I am taking it to mean you want to find common numbers: [code]shared_numbers = [] for number in set(reduce(lambda a, b: a+b, map(lambda y: map(lambda x: x, y), a))): if number in set(reduce(lambda a, b: a+b, map(lambda y: map(lambda ...
๐ŸŒ
Intellipaat
intellipaat.com โ€บ community โ€บ 11860 โ€บ how-can-i-compare-two-lists-in-python-and-return-matches
How can I compare two lists in python and return matches - Intellipaat Community
July 17, 2019 - I want to take two lists and find the values that appear in both. a = [1, 2, 3, 4, 5] b ... 5] returnMatches(a, b) would return [5], for instance.