loop only to the 6th element python

Solutions on MaxInterview for loop only to the 6th element python by the best coders in the world

showing results for - "loop only to the 6th element python"
Giuseppe
22 Jan 2021
1sample = "This is a string"
2n = 3 # I want to iterate over every third element
3for i,x in enumerate(sample):
4    if i % n == 0:
5        print("do something with x "+x)
6    else:
7        print("do something else with x "+x)
8
Gaia
04 Mar 2020
1import itertools
2for s in itertools.islice(sample,None,None,n):
3    print(s)
4