How can I find every nth element of a list?
For a list [1,2,3,4,5,6], returnNth(l,2) should return [1,3,5] and for a list
[1,2,3,4,5,6]
returnNth(l,2)
[1,3,5]
You just need lst[::n].
lst[::n]
Example:
>>> lst=[1,2,3,4,5,6,7,8,9,10] >>> lst[::3] [1, 4, 7, 10] >>>