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])
19 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

8 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)
15 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?

1 Like

the range was called using the length of a list, which is a number. If called using string data it would result in a type error.

1 Like

Why this example uses range to print items in the list? Is it just to present range logic?
This line does exatly the same, so why over complicate?

examples = [ā€˜redā€™, ā€˜greenā€™, ā€˜blueā€™, ā€˜orangeā€™]
for x in examples:
print(x)

so my solid question is how does the last example work? : print(range(len(fruits))) # prints range(0, 4)
how does this work if range should be -1 of the elements in the list ie: print(range(len(fruits))) # prints range(0, 4) should return 3 items not four should it not?
please clarify this

Adding to that, When you put len( of something ), remember that instead of starting from zero, it will start from one.
And if you put Len(1,2,3,4) - it will bring to an error since len (_) only defined strings, thatā€™s why we use the list() function to tell Len(-), Hey the numbers [1,2,3,4], are part of a list, which are considered strings.

For example :

Fruits = [[ā€œappleā€], [ā€œbananaā€], [ā€œorangeā€]]
Print(len(Fruits))
3