How To Get Every nth Element From A List in Python

In this tutorial, you will learn to get every nth element from a list in Python.

In Python, you can use the slicing method of a list to get every nth element from the list. List slicing is a technique that allows you to extract every nth element from a list.

1. Using the List Slicing Method

As discussed above, you can use the slicing method of the list in Python. Each nth element in a list is returned, along with all of the other elements whose indexes are multiples of n, starting with the first one. For example, taking every third element from the range [0, 1, 2, 3] results in the elements [0, 3].

Syntax:

List[start:stop:step]

Where, 
step = Every other element that you want to select.

Let us see in the below example code the usage of the Slicing Method in Python.

#creating a list of numbers
newList = list(range(100))

#Get the Fifth Element from the List
getEveryFifthNumber = newList[::5]

#Printing the List having every fifth
#Element from the List
print(getEveryFifthNumber)

Output:

[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

As you can see in the above code, I was able to select all the fifth elements from the list of numbers starting from 0 to 100 using the slicing method of the list. I have used the step in the list as 5 that indicated to select every 5th element in the list.

Note that this is the fastest solution that you can use.

2. Using Itertools.islice Method

You can also use the itertools.islice[1] method that does the same type of slicing as mentioned above. Though this method is memory efficient it is not as fast as the first method discussed.

Syntax:

itertools.islice(list, start, stop, step)

Where,
Step = it is similar to the list slicing step as shown above.

Let us see in the below example code the usage of itertools.islice() method to get the nth element from the list.

#creating a list of numbers
from typing import Iterable


#Importing itertools
from itertools import islice

newList = list(range(100))

#Get the Fifth Element from the List
getEveryFifthNumber = islice(newList, 0, None, 5)

#Printing the List having every fifth
#Element from the List
print(list(getEveryFifthNumber))

Output:

[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

As you can see in the above code, I was able to get the list of numbers present as the fifth element in the list using the itertools.islice method.

Wrap Up

Hope you were able to grasp the concept of how to get each and every nth element from a list using Python. I’ve provided two different approaches. First, the fastest method is also the least memory-efficient, while the second method is slower than the first but is more memory efficient than the first method.

Let me know in the comment section if you know any method better than the one discussed above I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Run Python Script Constantly In Background
  2. How To Convert Python String To Array
  3. Code Example: How To Check If Set Is Empty In Python
  4. Code Examples – Write A List To CSV Using Python
  5. Code Example: Loop Back To The Beginning Of A Program
  6. How To Do Nothing In An If Statement Python
  7. Code Example: Remove The First Item From List Python
  8. 4 Ways To Loop Through List With Index Using Python