Of course this will generate each pair twice as each for loop will go through every item of the list.

You could use some itertools magic here to generate all possible combinations:

import itertools
for a, b in itertools.combinations(mylist, 2):
    compare(a, b)

itertools.combinations will pair each element with each other element in the iterable, but only once.


You could still write this using index-based item access, equivalent to what you are used to, using nested for loops:

for i in range(len(mylist)):
    for j in range(i + 1, len(mylist)):
        compare(mylist[i], mylist[j])

Of course this may not look as nice and pythonic but sometimes this is still the easiest and most comprehensible solution, so you should not shy away from solving problems like that.

Answer from poke on Stack Overflow
🌐
Index.dev
index.dev › blog › compare-list-elements-python
How to Compare Two Elements of a List in Python (2026)
October 17, 2024 - Sometimes you do not need to compare pairs by hand at all. Python's built-in functions compare every element for you in fast C code. Use min() and max() for the smallest and largest values, and sorted() to order the whole list.
Discussions

comparing items in a list
if i == list2[i-1] Here, you're treating i as if it was both a number in the list and its index. But this would be impossible, and in this case the logic on the right side is wrong. Furthermore, this is checking for equality, not that the values are rising. zip and slicing would make the logic trivial: values = [1, 2, 2, 3, 3, 4, 4, 5] for previous, current in zip(values, values[1:]): if previous >= current: # Use > instead if the previous number cannot be equal print("Not rising") break else: print("Rising") More on reddit.com
🌐 r/learnpython
5
2
April 28, 2023
loops - Compare the elements of a list in python - Stack Overflow
I want to iterate through a list and want to compare the elements of list. For example: First element will be compared with next element. I've a list a: for i in range(len(a)) for i+1 in rang... More on stackoverflow.com
🌐 stackoverflow.com
Compare list elements in python - Stack Overflow
I have a list and want to compare if the last value is greater than the past 10 values, however, I know there is a much easier way to approach this then the code below: list = [1,2,3,4,5,6,7,8,9,1... More on stackoverflow.com
🌐 stackoverflow.com
Comparing all elements of a list with each other, without nested loops?
Like people said, the problem is comparing everything with everything else. If you want to speed up, you could try find approach where you don't have to do that. That said, you could simplify nested loop (it wouldn't be faster, actually may be slower to some extent, time complexity should be the same) with itertools.combinations : from itertools import combinations numbers = [1, 2, 3, 4, 5] for i, j in combinations(numbers, r=2): print(i, j) More on reddit.com
🌐 r/learnpython
6
2
September 9, 2022
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-compare-two-lists-in-python
How to Compare Two Lists in Python | DigitalOcean
July 22, 2025 - Use the == operator for the simplest and fastest check to see if two lists are identical in both content and order. For comparing list contents regardless of order, collections.Counter is the most efficient and reliable method as it correctly handles duplicate elements.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-compare-adjacent-elements-in-a-list-in-python
Compare Adjacent Elements in a List in Python - GeeksforGeeks
May 12, 2026 - Explanation: for i in range(len(li) - 1) loop iterates over the indices of the list li, stopping at the second-to-last element (len(li) - 1). This ensures that each element is compared with the next one without going out of bounds.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Compare Lists in Python | note.nkmk.me
May 10, 2023 - Each element is compared using ==. If the values are equal even if the types are different, it returns True. The method to determine if they are exactly the same, including the type, will be described later.
🌐
TutorialsPoint
tutorialspoint.com › how-to-compare-two-lists-in-python
How to compare two lists in Python?
The simplest method compares lists element by element in the same positions. This method considers both values and order ? def compare_lists(list1, list2): if list1 == list2: return "Equal" else: return "Not equal" # Same elements, same order numbers1 = [1, 2, 3] numbers2 = [1, 2, 3] print("Same order:", compare_lists(numbers1, numbers2)) # Same elements, different order numbers3 = [1, 2, 3] numbers4 = [2, 1, 3] print("Different order:", compare_lists(numbers3, numbers4))
Find elsewhere
🌐
LabEx
labex.io › tutorials › python-how-to-compare-multiple-list-elements-431031
How to compare multiple list elements | LabEx
Python offers multiple methods to compare list elements, each with unique characteristics and use cases. Understanding these methods helps developers choose the most appropriate approach for their specific requirements. ## Basic equality comparison list1 = [1, 2, 3] list2 = [1, 2, 3] list3 = [3, 2, 1] print(list1 == list2) ## True print(list1 == list3) ## False
🌐
Delft Stack
delftstack.com › home › howto › python › compare lists python
How to Compare Lists in Python | Delft Stack
March 11, 2025 - This tutorial demonstrates how to compare lists in Python using various methods such as the equality operator, sets, list comprehensions, and the collections.Counter class. Learn to identify common elements, unique items, and handle duplicates effectively.
🌐
Miguendes
miguendes.me › python-compare-lists
The Best Ways to Compare Two Lists in Python
July 2, 2022 - If we have a list of floats and want to compare it with another list, chances are that the == operator won't help. Let's revisit the example from the previous section and see what is the best way of comparing two lists of floats.
🌐
Java2Blog
java2blog.com › home › python › python list › compare list elements with each other in python
Compare List Elements with Each Other in Python - Java2Blog
December 2, 2023 - Use Case: Ideal for small lists or where performance is not a critical concern. itertools.combinations efficiently generates all pairs of elements for comparison. ... itertools.combinations: This is a function from Python’s built-in module itertools. It is used to create all possible combinations of elements from the input list.
🌐
Quora
quora.com › How-do-I-compare-the-elements-within-a-list-in-Python-3
How to compare the elements within a list in Python 3 - Quora
Answer: You can compare two or more elements individually in a list.Suppose a=[1,2,3,4,5,6,7,8] is your list. Now a[i] will be the elements of the list where i is any number starting from 0 to the length of the list. Note:- a[0] will give the first element of the list ( in this case a[0] is 1)....
🌐
LabEx
labex.io › tutorials › python-how-to-compare-elements-across-lists-451188
Python - How to compare elements across lists
By understanding these comparison strategies, you'll be able to handle complex list manipulation tasks with ease and efficiency. from difflib import SequenceMatcher def fuzzy_list_match(list1, list2, threshold=0.6): """ Perform fuzzy matching between two lists :param list1: First list of elements :param list2: Second list of elements :param threshold: Similarity threshold :return: Matched elements """ matches = [] for item1 in list1: for item2 in list2: similarity = SequenceMatcher(None, str(item1), str(item2)).ratio() if similarity >= threshold: matches.append((item1, item2, similarity)) return sorted(matches, key=lambda x: x[2], reverse=True) ## Example usage names1 = ['John', 'Sarah', 'Michael'] names2 = ['Jon', 'Sara', 'Michel'] fuzzy_matches = fuzzy_list_match(names1, names2) print(fuzzy_matches)
🌐
Scaler
scaler.com › home › topics › python list comparison
Python List Comparison - Scaler Topics
May 4, 2023 - Using == is not the answer in every circumstance; yet, for certain specific problems, it is! The equality operator == compares a list element by element. ... In Python, you can sort lists in two methods.
🌐
Java2Blog
java2blog.com › home › python › python list › how to compare lists in python
How to compare lists in Python [8 ways] - Java2Blog
January 13, 2022 - We implement this logic in the following code snippet. ... The cmp() function returns 1,-1, and 0 based on the comparison between two integers. We can use it with lists, and if the result is 0 then the two lists are equal. This function works only in Python 2.
🌐
Finxter
blog.finxter.com › home › learn python blog › the most pythonic way to compare two lists in python
The Most Pythonic Way to Compare Two Lists in Python - Be on the Right Side of Change
June 27, 2020 - But true Python coders will often avoid a for loop and use a generator expression instead. You first create an iterable of Boolean values using the generator expression x == y for x, y in zip(l1, l2).
🌐
Mouse Vs Python
blog.pythonlibrary.org › home › determining if all elements in a list are the same in python
Determining if all Elements in a List are the Same in Python - Mouse Vs Python
August 13, 2021 - As you can see, its work is very similar to that of the set() function, with the only difference that in this case the object type doesn't change - the list remains the list. The solution with this function looks like this: from numpy import unique def all_the_same(elements): return len(unique(elements)) <= 1 · 4. Here is an example of a very original solution, in which the used Python's standard all() function is the play on the name of this task.
🌐
Javatpoint
javatpoint.com › how-to-compare-two-lists-in-python
How to compare two lists in Python - Javatpoint
How to compare two lists in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.