Update formatting in every_nth Update formatting in shuffle Update formatting in has_duplicates Update formatting in group_by Update formatting in sum_by Update formatting in zip Update formatting in longest_item Update formatting in bifurcate_by Update formatting in difference_by Update formatting in clamp_number Update formatting in min_by Update formatting in max_by Update formatting in union Update formatting in n_times_string Update formatting in check_prop Update formatting in chunk Update formatting in transpose Update formatting in bifurcate Update formatting in union_by Update formatting in initialize_list_with_range Update formatting in most_frequent
18 lines
296 B
Markdown
18 lines
296 B
Markdown
---
|
|
title: every_nth
|
|
tags: list,beginner
|
|
---
|
|
|
|
Returns every nth element in a list.
|
|
|
|
Use `[nth-1::nth]` to create a new list that contains every nth element of the given list.
|
|
|
|
```py
|
|
def every_nth(lst, nth):
|
|
return lst[nth - 1::nth]
|
|
```
|
|
|
|
```py
|
|
every_nth([1, 2, 3, 4, 5, 6], 2) # [ 2, 4, 6 ]
|
|
```
|