Table of Contents

In this post, we will see how to compare list elements with each other in Python.

## Compare all elements in list Python

A list is a very useful iterable in Python. Every element is stored at a given index and can be accessed using this index.

We can compare all elements in a list in Python. We can have different situations for this and the methods can vary depending on the required result. In this article, we will demonstrate how to compare each element in a list with every other element at least once.

## Ways to compare list elements with each other in Python

The different methods are discussed below. We take a generic case of comparing elements with each other at least once.

### Using the `for`

loop to compare all elements in list Python

We can iterate over a list using the `for`

loop. To compare all elements with each other, we will use a nested loop. A nested loop is a loop in another loop.

The outer loop will take one element and the inner loop will compare it to every other element. For our example, we will check whether an element is less than other elements of the list. We can perform any comparison we desire, and can also substitute it for a user-defined comparison function.

We will store the result for every comparison in the list which will be displayed after the nested loop ends. This method will display a different list for every element.

See the code below.

1 2 3 4 5 6 7 8 9 10 11 |
lst = [1,5,8,9,6] for i in lst: res = [] idx = lst.index(i) for j in range(len(lst)): if(idx == j): continue res.append(i < lst[j]) print(res) |

Output:

Let us understand what is happening in the above code.

- We defined a list with five elements and we wish to compare every element with each other, so there will be four comparisons.
- We define a
`for`

loop to iterate through the list and store the index for this element in the`idx`

variable. - We now create a nested loop to again iterate through the list using its index.
- In this loop, we check the index of the element at the outer loop with the iterating index to avoid comparing the element with itself.
- The
`continue`

statement forces the next iteration. - We then perform the comparison, store the result in the list
`res`

and display it after the nested loop ends.

We can also encounter simpler situations for comparison. For example, if we wish to find the smallest element in a list, we can do so with a single `for`

loop.

We will assign the first element as smallest and with every iteration compare this variable to every element of the list, updating it if it encounters a value smaller than itself.

See the code below.

1 2 3 4 5 6 7 8 |
lst = [1,5,8,9,6] a = lst[0] for i in lst: if(a>i): a = i print(a) |

Output:

## Further reading:

### Using the `itertools.combinations()`

function to compare all elements in list Python

In Python, we can work with iterables and produce more complex iterables using the `itertools`

library.

The `itertools.combinations()`

function applies the mathematical combination formula and can be used to form all possible combinations of elements from an iterable, by taking a given number of elements of the time.

We will create all the possible unique pairs of elements from the list by using the `itertools.combination()`

function. We will compare this pair based on our criteria and display the result.

1 2 3 4 5 |
import itertools for a, b in itertools.combinations(lst, 2): print(a, '<' , b, a<b ) |

Output:

1 < 8 True

1 < 9 True

1 < 6 True

5 < 8 True

5 < 9 True

5 < 6 True

8 < 9 True

8 < 6 False

9 < 6 False

In the above example, we create unique pairs from the list using the `itertools.combinations()`

function and compare every pair and print the result of each comparison.

## Conclusion

To wrap up, we discussed how to compare list elements with each other in Python. The first method involves the use of nested loops to compare an element with every other element of the list.

We used the `itertools.combinations()`

function in the second method to form unique pairs from the elements of the loop and compare them.

That’s all about how to compare list elements with each other in Python.