Compare Two Consecutive Elements in List in Python (2 Examples)

In this tutorial, you’ll learn how to compare two consecutive elements in a list in Python Programming Language. Comparing consecutive elements involves comparing each element with its adjacent element in the list. We’ll explore different methods to achieve this comparison.

The table of contents is structured as follows:

Let’s get started!

Initializing Sample List

Firstly, let’s initialize a sample list:

# Initializing a sample list
my_list = [1, 3, 5, 2, 4, 6]

This creates a list, my_list, containing some elements.

Example 1: Comparing Each Consecutive Pair

One way to compare two consecutive elements in a list is by iterating through the list and comparing each element with its adjacent element. Here’s an example:

# Comparing each consecutive pair
comparisons = [1 if my_list[i] > my_list[i+1] else -1 if my_list[i] < my_list[i+1] else 0 for i in range(len(my_list)-1)]
 
print(comparisons)
# [1, 1, -1, -1, -1]

In this example, we use a list comprehension to iterate through the list using the index i. We compare my_list[i] with my_list[i+1] and assign the result (1 if the first element is greater, -1 if the second element is greater, or 0 if they are equal) to the comparisons list. The output displays the content of comparisions.

Example 2: Checking Equality of Each Consecutive Pair

Another way to compare two consecutive elements in a list is by checking if consecutive pairs are equal. Here’s how you can do it:

# Checking if all consecutive pairs are equal
if_equal = [my_list[i] == my_list[i+1] for i in range(len(my_list)-1)]
 
print(if_equal)
# [False, False, False, False]

In this example, we use list comprehension again to iterate through the list and check if each consecutive pair is equal and display the result.

If you are interested in checking if all consecutive elements are equal only, then you should employ the all() function and replace the square brackets [] with parenthesis () to use a generator expression. See the code snippet below.

# Checking if all consecutive pairs are equal
all_equal = all(my_list[i] == my_list[i+1] for i in range(len(my_list)-1))
 
print(all_equal)
# False

Now you have the knowledge and techniques to compare two consecutive elements in a list in Python. Happy coding!

Video, Further Resources & Summary

In this tutorial, we explored different methods to compare two consecutive elements in a list in Python. We learned how to compare each consecutive pair and how to check if each consecutive pair is equal. These techniques can be helpful in scenarios where you need to analyze the relationships between adjacent elements in a list.

Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube chan

The YouTube video will be added soon.

For more Python programming tutorials and examples, you can check out the following resources on Statistics Globe:

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his 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.

Top