0

Say i have this list:

CP = [1,0,1,0]

How can i print only 0's in the list via indices i.e CP[i].

Required output:

0
0

Any help would be highly appreciated!

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Jagadeesh Kotra
  • 194
  • 1
  • 3
  • 9

2 Answers2

3

This feels like a homework problem, and I assume you actually want odd indices instead of even indices.

Here's a possible solution:

# get a range of all odd numbers between 1 and length of list
# with a step size of 2
for i in range(1, len(CP), 2):
    print(CP[i])
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

The one liner --

print ('\n'.join([str(CP[i]) for i in range(1, len(CP), 2)]))
Easton Bornemeier
  • 1,918
  • 8
  • 22