Find Pattern in List in Python (3 Examples)


Hi! This short tutorial will show you how to detect patterns in a list in the Python programming language.

Here is an overview:

Let’s jump into the Python code!


Create Example List

Here, we will create an example Python list of integer values that we will use in this tutorial.

So, in your preferred Python coding IDE, run the code below to create the example list:

my_list = [2, 5, 8, 11, 14, 17, 20, 8, 11]
# <class 'list'>

With the example list created, we will now look at examples of patterns we can uncover in the list.

Example 1: Detect Even & Odd Numbers in List

In this first example, we will detect any even and odd number occurences in the list:

even_numbers = []
odd_numbers = []
for i in my_list:
  if i % 2 == 0:
  elif i % 2 != 0:
# [2, 8, 14, 20, 8]
# [5, 11, 17, 11]

In the above example, we first initialized two empty lists, even_numbers and odd_numbers, to store even and odd numbers from the original list, respectively.

Then, we iterated through each element in my_list and used the modulo operator % to check if the element is divisible by 2 without leaving a remainder.

If it is divisible by 2, the element is considered even and is added to the even_numbers list; otherwise, if it leaves a remainder, the element is considered odd and is added to the odd_numbers list.

Example 2: Detect Duplicates in List

In this next example, we will detect any duplicate elements in the list and store them in a separate list:

seen = set()
duplicates = []
for element in my_list:
    if element in seen:
# [8, 11]

Here, we begin by initializing an empty set called seen and an empty list named duplicates. Then, we run a for loop that iterates through each element in my_list.

Within the loop, we check if the current element is already present in seen. If the element is found in seen, it implies that the element has been encountered before and is thus a duplicate.

In this case, the element is added to duplicates. If the element is not in seen, it means it’s a new element, so it’s added to seen to keep track of it.


Example 3: Detect Smallest & Largest Numbers in List

In this last example, we will detect the smallest and the largest numbers in the list:

smallest_number = min(my_list)
largest_number = max(my_list)
# 2
# 20

In the example above, we use the min() function to determine the smallest value within the list and assign it to the variable smallest_number.

Similarly, the max() function is employed to find the largest value in the list, which is then assigned to the variable largest_number.

Video, Further Resources & Summary

Do you need more explanations on how to find patterns in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain how to find patterns in a list in Python.


The YouTube video will be added soon.


Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find patterns in a list in Python. I hope you enjoyed reading this tutorial! In case you have further questions, you may leave a comment below.


R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.


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.