We can also use the len() function to find out how many elements a list is currently holding.
my_list = [500, 1999, 4, 800, 900] number_of_elements = len(my_list) print( number_of_elements ).
Because there are five elements in the list (500, 1999, 4, 800 and 900), the code above should print the number 5.
Each element of the list above gets a number called its index: the first element is at index 0, the next element is at index 1, and so forth. We can see this in the following example:
Accessing a List
In addition to being able to add elements to a list, you can change the current elements by using the index of the element you would like to change.
Changing List Elements
Sometimes you will find yourself accessing an index that does not exist. See the error below
Traceback (most recent call last):
File "python", line 6, in <module>
IndexError: list index out of range
The first line tells us that an error occurred.
The second line says that it happened in this file on line number 6.
The third line explains that it was an index error, the index used on line 6 does not exist in the list.
Index Error