Range command confusion

Screen Link:
https://app.dataquest.io/m/1009/lists-and-for-loops-practice-problems/5/incrementing-all-elements

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]

Write your answer below

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]

Write your answer below

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

Hello @Gidi.ekong,

From your question, I could identify this as your code block:

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[0] += 1

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

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

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

up till

for 19 in the sequence of numbers from 0 -19:
values[19] += 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.
click here to learn more about list.

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[0] = 16 , values[1] = 1, values[2] = 7, values[3] = 2 up till values[19] = 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. :grinning: :grinning: :grinning:
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[0]
'spam'
>>> a[2]
'bacon'
>>> a[5]
'lobster'

also in

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

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

values[0] = values[0] + 1
values[1] = values[1] + 1
values[2] = values[2] + 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 :arrow_down: 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] += 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[2] += 1

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

I hope this helps.

1 Like

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

1 Like