How to count list elements with values in a given range with Python

1 Answer

0 votes
def countValuesInRange(lst, from_, to_):
        count = 0
        size = len(lst)
        for i in range(size):
            if lst[i] >= from_ and lst[i] <= to_:
                count += 1
        return count

       
       
lst = [  3, 6, 0, 17, 9, 1, 8, 7, 4, 10, 18, 11, 20, 30 ]
from_ = 5
to_ = 12
  
print(countValuesInRange(lst, from_, to_))

 
     
'''
run:

6

'''

 



answered Dec 16, 2021 by avibootz

Related questions

...