How do I print list elements which are greater than a certain number


I have a list and I want to print the no of elements greater than 5, I tried like this

l = [1,2,6,7,8]
for i in range(0,4): 
   if i > 5:
      print(i)

the output is 6 7
but I wanted it to be as 6 7 8
How can I fix that?

1 Answer

4 years ago by

In a for loop, the loop will be iterated till before the last value. So, here you have the outer limit as 4, so the loop will be iterated till 3. so to fix your issue you need to change the ending limit to 5

l = [1,2,6,7,8]
for i in range(0,5): 
   if i > 5:
      print(i)
4 years ago by Divya