# Range command confusion

My Code:

``````Replace this line with your code
``````

What I expected to happen:

What actually happened:

``````Replace this line with the output/error
``````

values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 10, 14, 17, 14, 1, 16, 19, 7, 9, 19]

for i in range(len(values)):
values[i] += 1

please can someone explain the 4th and 5th lines of code to me.

Hello @Gidi.ekong,

Your question is not clear. What are the 4th and 5th lines of code?

i apologise about making it unclear
the 4th and fifth lines respectively are
for element in range(20):
values[element] += 1

for context the full code is;
values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 10, 14, 17, 14, 1, 16, 19, 7, 9, 19]

for element in range(20):
values[element] += 1
print(values)

Hello @Gidi.ekong,

``````values = [16,1, 7, 2, 19, 12, 5, 20, 2, 10, 10, 14, 17, 14, 1, 16, 19, 7, 9, 19]
for element in range(20):
values[element] += 1
``````

The `range(n)` function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default) up till “n-1”.
Hence, `range(20)` creates a sequence of numbers from 0 to19.

`for element in range(20):` means that for each item in `range(20)` (which is a sequence of numbers from 0 to 19), execute the code under the for loop which is `values[element] += 1`.

`element` therefore represents each number in the sequence of numbers from 0 to 19 for every time the code under the for loop is executed.

`values[element] += 1` means add 1 to the present value of `values[element]`.

Therefore, in the background the loop runs as:

for 0 in the sequence from 0-19:
values += 1

for 1 in the sequence of numbers from 0 -19:
values += 1

for 2 in the sequence of numbers from 0 -19:
values += 1

for 3 in the sequence of numbers from 0 -19:
values += 1

up till

for 19 in the sequence of numbers from 0 -19:
values += 1

It can, therefore, be concluded that your code adds 1 to every number in the `values` list.

The `print(values)` line of code will return `values = [17, 2, 8, 3, 20, 13, 6, 21, 3, 11, 11, 15, 18, 15, 2, 17, 20, 8, 10, 20]`

I hope this explains it well.

4 Likes

Thanks, this helped although i have some further understanding to do.

what does values[element] mean in the line values[element] +=1 ?

@Gidi.ekong

`values[element] ` in the code line `values[element] += 1` is a way of accessing the elements of the list `values`.

In Python, we use the index operator `[]` to access an item in a list. In Python, indices start at 0. So, a list having 20 elements will have an index from 0 to 19.

Also, our `element` variable holds a value from `0` up till `19` each time the `for loop` runs (as explained in my previous reply)

In our `values` list, `values = 16` , `values = 1`, `values = 7`, `values = 2` up till `values = 19`

Also, `values[element] += 1` means `values[element] = values[element] + 1`

2 Likes

So here basically len function is used to get the count. This count is determining till when code should execute.   Thanks for detailed explanation. (n question) Plz correct me if i am wrong.

You are correct!

Happy learning!

I am curious with this practice question, why the range(len(values)) is needed. I’m sure my understanding is wrong, but won’t a “for” loop iterate over each value in the values list?

So if I have a code like:

``````values = [1, 2, 3, 4, 5]
for i in values:
values[i] += 1

print(values)
``````

Would this not iterate over each number in the values list and add a 1 to the value? Just guess I need a better explanation as to why we have to figure out the length of a list first, then specify the range then start the iteration process?

I understand that the len function is used to get the count, and the count is determining when the code should execute. I just thought that the code that I wrote above was doing that? What is my code doing?

Thanks!

3 Likes

I have the exact same question. Could @doyinsolamiolaoye explain a bit more?

1 Like

Hello, @boemer00 @joshuaroberts.it,

Lists elements are accessed using a numerical index in square brackets ` []` which is known as List indexing. List Indexing is zero-based which means that the first index number is `0` and the last index number is the `number of elements in the list - 1`. It is demonstrated in the exmaple below:

``````a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
'spam'
>>> a
'bacon'
>>> a
'lobster'
``````

also in

``````values = [1, 2, 3, 4, 5,6]
>>> values
1
>>>  values
3
>>> values
6
``````

Therefore, to add a value of `1` to each element in the list `values`, we have to do

``````values = values + 1
values = values + 1
values = values + 1
up untill the last element in the list which is ...
values[last index number] = values[last index number] + 1
``````

Therefore we need to loop through a sequence of numbers which represents the index of each element in the list i.e a sequence of numbers starting from `0` up until the `number of elements in the list - 1`.

The `range(n)` function returns a sequence of numbers, starting from `0` by default, and increments by 1 (by default) up till “n-1”. `len()` helps us to know the number of elements in the list which will serve as the input parameter in the `range` function.

This explanation below further explains it

Now, the reason why your code won’t give the same result is because you are indexing the list with the elements of the list.

In the first loop of the code above, it adds a value of `1` to the `2nd` element in the list whose index is `1`.
The first loop will be:

``````for 1 in [1, 2, 3, 4, 5]: #takes the first element in the list
values += 1 #indexes the list with the first element (1) and adds the value of 1 to it
``````

Second loop will be :

``````for 2 in [1, 2, 3, 4, 5]:
values += 1
``````

This code will eventually run into an `IndexError` because at the last element of the list `5`, `values` does not exist. Only `values` up until `values` exists.

I hope this helps.

1 Like

Amazing, thank you so much for the detailed explanation. It was a tremendous help.

1 Like