# For loops and the range function

Hello! I’m sure this is quite basic but I can’t quite seem what to grasp how and when I should be using the range(len()) functions for for loops.

for example, in the conditional statements practice problem, we have this code for q.10:
values = [80, 109, 111, 109, 94, 93, 108, 107, 81, 111, 114, 102, 81, 107, 120, 108, 92, 113, 119, 97]
num_bigger = 0
for i in values:
if i > 100:
num_bigger += 1
print(num_bigger)

but for q.12 we have the following:
values1 = [80, 109, 111, 109, 94, 93, 108, 107, 81, 111, 101, 114, 102, 81, 107, 120, 108, 92, 113, 119, 97]
values2 = [97, 119, 113, 92, 108, 120, 107, 81, 102, 114, 110, 111, 81, 107, 108, 93, 94, 109, 111, 109, 80]
is_reversed = True

for i in range(len(values1)):
if values1[i] != values2[-i - 1]:
is_reversed = False
print(is_reversed)

Why is range() used for the one, and not the other?

I hope that this question makes sense!

Took me a while to remember this but here is: In the first one you iterate through values, the second one you iterate through indexes. Basically in the second one you are writing `for i in range(0,21)` but in real life you are not going to have 21 values, you are going to have hundreds or thousands of values.

In short you use range(len()) when you want to iterate through indexes. However as soon as you get more experience and read more code you are going to find more efficient ways to do that

hey @veenstramy

you haven’t attached a mission link, so I am not that clear about your query. Will still try to add to what @alegiraldo666 has mentioned.

in the first case: you are iterating through each and every element in `Values1` so for every loop the iterator `i` will take the value itself of each element like below:

``````1st loop > i = 80
2nd loop > i = 109
.
.
nth loop > i = 97
``````

in the second case: iteration takes the total length of the list given. Since the index in a list starts at 0 and default start of `range()` method is 0, second loop works like this:

``````1st loop > i = 0
2nd loop > i = 1
.
.
last loop > i = n - 1 (where n = length of List)
``````

as @alegiraldo666 mentioned, we then access each element in the two lists using `i` as the index position for an element.

In the first case, `range()` is not used because values is a list/array that you may not know the length (say if you are importing the data from an external dataset or you want to modify the length of the code, and you would need to modify the `range()` function everytime you append/remove a value from/to the array). Note that modifying code in 2 or more areas (in the case the values in the array and the `range()` function) is generally deemed to be a bad practice in computer science.

In the second case however, there are two lists/array of equal length. Hence, even if you may not be sure about the length of each array, you can confirm that `len(values1) == len(values2)`. Hence ‘range()’ can be used to index and compare the values in the loop.

Hope this clarifies.