Len(range) function

Hi! I just don´t really understand the meaning of the functions ‘len’ and ‘range’ together.
What is suposed to do with a list? I mean, in exercise 5 of List and For Loops Practice just don´t understand why it counts a range…

image
I think you are talking about this.
For loop always iterate over a range , be it a range function like (range(0,9) or range(9) they both are same, if you don’t define from where it should start , by default it will be zero, otherwise you can tell to iterate on other way too range can be range(4,8)), be it a list or a dictionary etc.
Here inside the range it took the length of the list or count of the elements of the list with len() to define how many times the loop will work. it gives the range to for loop range(20).

Otherwise you can follow my solution below here:

values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 10, 14, 17, 14, 1, 16, 19, 7, 9, 19]
for each_value in values:
each_value += 1
print(values)
@ javiermarzalb, If you find it useful, please mark my answer as solution.

1 Like

Hi @javiermarzalb:

You may one to check out this article I answered.

1 Like

Ok, so in this solution, it´s not necessary to use ‘len’ function…The first time I tried, I did something similar but it was not right.

Thank you!

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

new_values = [ ]

for each_value in values:

    each_value += 1 
    new_values.append(each_value)

values = new_values
@javiermarzalb please check it out.

1 Like

image

I am so sorry @javiermarzalb, if the previous code didn’t work, and this code is tested, I’m so sorry for the inconvenience.