Python - Visit every second item in sequence

Introduction

We might use range and len to skip items as we go:

Demo

S = 'abcdefghijk' 
print( list(range(0, len(S), 2)) )

for i in range(0, len(S), 2): print(S[i], end=' ')

Result

Here, we visit every second item in the string S by stepping over the generated range list.

To visit every third item, change the third range argument to be 3, and so on.

Related Topic