How to loop over every two elements in a list with Python

2 Answers

0 votes
lst = [1,2,3,4,5,6,7,8,9,10,11,12]

for i in lst[::2]:
    print(i)



     
     
'''
run:
     
1
3
5
7
9
11
  
'''

 



answered Apr 23, 2021 by avibootz
0 votes
lst = [1,2,3,4,5,6,7,8,9,10,11,12]

for i in lst[1::2]:
    print(i)



     
     
'''
run:
     
2
4
6
8
10
12
  
'''

 



answered Apr 23, 2021 by avibootz

Related questions

...