Most questions about a list are finished by walking it once from start to end. The trick is to carry what you need in a variable as you go: a sum, a counter, a best.
total = 0for v in values:if low <= v <= high:total += vreturn total
low <= v <= high makes both comparisons at once: is v inside the range? Both ends are included.On this site every read of an element is counted. Walking a list of n elements once is n reads; walking the list again for each element is n × n. When the list grows 10 times, the first gets 10 times slower and the second 100 times.