Get every Nth element in a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Get every Nth element in a List in Python #

Use list slicing to get every Nth element in a list, e.g. result = my_list[::2]. The value between the square brackets is the step which can be used to select every Nth element in the list.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ✅ get every second element starting at the second element n = 2 result = my_list[n - 1::n] print(result) # 👉️ [2, 4, 6, 8] # ✅ get every second element starting at index 0 result = my_list[::2] print(result) # 👉️ [1, 3, 5, 7, 9] # ------------------------------------------- # ✅ get every third element starting at index 0 result = my_list[::3] print(result) # 👉️ [1, 4, 7] # -------------------------------------------- # ✅ get every second element starting at index 2 result = my_list[2::2] print(result) # 👉️ [3, 5, 7, 9]

We used list slicing to get every Nth element in a list.

If you need to select every Nth element in the list starting at the Nth element, e.g. get every second element in a list, starting at the second element, specify the start index.
main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ✅ get every second element starting at the second element n = 2 result = my_list[n - 1::n] print(result) # 👉️ [2, 4, 6, 8]

The start index is n - 1, so we start selecting elements from the Nth element onwards.

If you want to get every Nth element in a list starting at index 0, omit the start index.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # # ✅ get every second element starting at index 0 result = my_list[::2] print(result) # 👉️ [1, 3, 5, 7, 9]

The syntax for list slicing is my_list[start:stop:step]. The value for the start index is inclusive, whereas the value for the stop index is exclusive.

To get every Nth element in a list, we only specified a step value.

When the start and stop indexes are omitted, the slice starts at the beginning (index 0) of the list and goes to the end.

The step value is set to 2 in the example, so we get the list items at index 0, 2, 4, 6, etc.

In other words, we select every second element starting at index 0.

If the step is set to 3, the list slice selects every third list item.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] result = my_list[::3] print(result) # 👉️ [1, 4, 7]

The step value is set to 3, so we select the list items at index 0, 3, 6, 9, etc.

When the start index is omitted, we start selecting items at index 0.

If you want to get every Nth element in a list starting at a specific index, provide a value for the start index.

Here is an example that starts at index 2 and selects every second element in the list.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] result = my_list[2::2] print(result) # 👉️ [3, 5, 7, 9]

The list slice starts at index 2 and we select every second item (2, 4, 6, etc).

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.