How does the len() function relate to index values for lists?

Question

What is the relationship between the value returned by len() and the indexes which can be used to access elements of a list?

Answer

The len() function returns the number of items in a list. Since list indexes start at 0, the value of len() represents a value one larger than the last index. This means the valid indexes of a list are any number between 0 and len() - 1. The len() function is commonly used with the range() function. Since range() by default, returns a list from 0 to one less than the number passed, the len() of a list can be passed to range() to generate a valid list of indexes. This is shown in the example below.

examples = ['red', 'green', 'blue', 'orange']

# This will print 4 which is the number of elements in examples.
print(len(examples))

# This will return a list ([0, 1, 2, 3]) which provides indexes to the examples.
for color in range(len(examples)):
   print(examples[color])
18 Likes

I’m not quite understanding the final two lines in the example. When I run the code, the colours are printed and not [0, 1, 2, 3]. Am I missing something?

1 Like

i you would do:

print(color)

you would get the numbers produced by range.

but by doing examples[color], you use the numbers as index to access the values of the list

5 Likes

Why does color work? I replaced the colors with items like "books", "wedding", "sad", "song" and the code still ran.

as string? the iterator should be a variable. The iterator is defined along with the loop. So it doesn’t matter what you name the variable, but its recommend to choice a logical name

I tried different variations of functions range, list, and len. Maybe it’ll help someone. :upside_down_face:

fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’]

print(list(range(len(fruits)))) # prints [0, 1, 2, 3]

print(len(list(fruits))) # prints 4

print(range(len(fruits))) # prints range(0, 4)
6 Likes

To add to the above point, in the context of -
for color in range(len(examples)):
print(examples[color])

the for loop “iterates” over the range of values within the “example” list using the variable “color”. The name “color” does not matter as long as the loop has an iterator defined.

I do not quite understand why I did not need to convert the range object to a list when calculating the length in this exercise. In the code section, we were provided:

list1 = range(2, 20, 2)

…which I understood to be a range object from earlier lessons. For example:

print(list1)
print(type(list1))

range(2, 20, 2)
<class 'range'>

For that reason, I thought this is how I would calculate the length of the “list”:

len(list(list1))

…but the correct way is apparently:

len(list1)

Why is that?

range produces a range object in python3, however, in python2, range gives a list. Which exercise are you currently at? Could you provide the url/link?

https://www.codecademy.com/courses/learn-python-3/lessons/use-python-list/exercises/list-len

Been a while since I done python, but seems len() can handle many different arguments:

https://www.programiz.com/python-programming/methods/built-in/len

including a range object

1 Like

Hi a_suslov,

I do not quite understand the motivations behind running a ‘range’ function in a list which comprises of only string.

(And the reason for me to say that is: I had seen range being used primarily with numbers when the starting and the ending point for the list could be too long.
For eg:
long_list = range(0, 26)
print(long_list) => Would yield a list of integers from 0 to 25.)

How can range help us give an output with lists made of string data? If not, could you tell me why you have tried multiple combinations with range function on string data?