Compare Values within List in Python (Example)


This article illustrates how to compare elements within a list in the Python programming language. To be more precise, the article will consist of the following content blocks:

With that, let’s just jump right in.


Importing Module & Creating Example Data

First, we need to import the itertools module since we will need its functionality in this tutorial.

import itertools                                      # import itertools

As a next step, we will create a sample list for demonstration.

mylist = [1, 2, 3, 4, 2]                              # create sample list
print(mylist)                                         # print mylist
# [1, 2, 3, 4, 2]

In the next section, we will compare each element pair in mylist. Without further ado, let’s jump into the example!


Example: Compare Elements within List

First, we will define a function assessing pairs of elements and returning the output: lower, higher, or equal based on the comparison.

def compare(a, b):                                    # define comparison function
    if a < b:
        return "lower"
    elif a > b:
        return "higher"
        return "equal"

If you are familiar with if-else statements and user-defined functions in Python, the previous step should be a simple piece of code for you. If not, please click on the highlighted terms above.

The next step is to set a for loop to iterate through all possible pairs in mylist. To extract all possible pairs in a list, we will employ the combinations function of the itertools module.

This for loop will print an f string returning the elements being compared and the result of the comparison for each element pair. Here you go!

for a, b in itertools.combinations(mylist, 2):        # iterate through each pair
    print(f"Comparing {a} and {b}: {compare(a, b)}")  # print comparison result
# Comparing 1 and 2: lower
# Comparing 1 and 3: lower
# Comparing 1 and 4: lower
# Comparing 1 and 2: lower
# Comparing 2 and 3: lower
# Comparing 2 and 4: lower
# Comparing 2 and 2: equal
# Comparing 3 and 4: lower
# Comparing 3 and 2: higher
# Comparing 4 and 2: higher

As seen above, we got the comparison for all elements within mylist. Well done!


Video & Further Resources

Some time ago I have published a video on my YouTube channel, which shows the contents of this tutorial. Please find the video below:


The YouTube video will be added soon.


Besides that, you may read the related posts on my website. I have released several tutorials on similar topics such as indices, data conversion, lists, and groups:


At this point you should know how to evaluate values against each other in the Python programming language. Don’t hesitate to tell me about it in the comments, in case you have any further comments or questions.


Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.