0
0
PythonHow-ToBeginner · 3 min read

How to Get Every nth Element from List in Python

To get every nth element from a list in Python, use list slicing with the syntax list[start::n]. This means start at index start (usually 0) and take every nth element until the end of the list.
📐

Syntax

The syntax to get every nth element from a list is:

list[start::n]
  • list: your original list
  • start: the index to start from (default is 0)
  • n: the step size, which means take every nth element

This uses Python's slicing feature where the third value is the step.

python
my_list = [10, 20, 30, 40, 50, 60, 70, 80]
every_3rd = my_list[0::3]
print(every_3rd)
Output
[10, 40, 70]
💻

Example

This example shows how to get every 2nd element from a list starting from the first element (index 0).

python
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
every_2nd = numbers[0::2]
print(every_2nd)
Output
[1, 3, 5, 7, 9]
⚠️

Common Pitfalls

One common mistake is forgetting the step value and using only two colons or one colon, which won't skip elements.

Another is starting at the wrong index, which changes which elements you get.

Also, negative step values reverse the list, which is a different use case.

python
my_list = [10, 20, 30, 40, 50]
# Wrong: missing step, returns full list
print(my_list[0:5])

# Right: step of 2 to get every 2nd element
print(my_list[0::2])
Output
[10, 20, 30, 40, 50] [10, 30, 50]
📊

Quick Reference

SyntaxDescriptionExample Output
list[::n]Get every nth element from start to end[10, 40, 70]
list[start::n]Get every nth element starting at index start[20, 50, 80]
list[::-n]Get every nth element in reverse order[80, 50, 20]

Key Takeaways

Use list slicing with a step value to get every nth element: list[start::n].
The start index defaults to 0 if omitted, meaning from the beginning of the list.
Forgetting the step or using incorrect slicing syntax returns the full list or unexpected results.
Negative step values reverse the list and then take every nth element.
Always test with print statements to confirm you get the expected elements.