# 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.

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

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

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