Continuing with our previous example, run this set of commands in Repl.it:
The elements contained within a list can be referenced using what is known as an index. Notice that Python begins indices by starting at a value of zero. So, if a list has 10 elements, the first element on the list is referred to using an index of 0, and the last element is referred to with a value of 9. This can take some getting used to if you are used to counting starting with the number 1.
A common error when first starting with lists is attempting a command such as:
on a list with 10 elements. Such a command would yield an error message and halt program execution because there is no such element.
The index is the key to referring to an element within a list. You must see the programming equivalence between an element and referencing the element via its index. Continuing with our example:
c=3 + alist_examp
alist_examp= c +alist_examp
The whole point of using a list is that a programmer plans to reference elements further down in a program. In this case, 3 is being added to the element with index 1, and then that element is being assigned a new value by referencing the element with index 0. Mastering the gymnastics of using indices is key to becoming an advanced programmer.
In this example, we explicitly typed out all the indices of every element. What if we had a list with 1000 elements, and we wanted to output them all one-by-one? Would we have to type 1000 commands? If we did, we would be completely disregarding the power of loops, which we mastered in the previous unit. Consider this code as an alternative:
for i in range(len(alist_examp)):
print('Element',i, '= ', alist_examp[i])
Notice how the result of the
len command is being used to define the range of the loop. Lists, indexing, and loops are related topics. It is important to understand how they are related to become a seasoned programmer.
One more important point must be mentioned about list indexing. Recall that any valid data type in Python can be inserted into a list. Lists are a valid data type; therefore, it is possible to have a list that contains lists as this example shows:
z=[x , y, 3.45678]
print('The list z contains ', len(z),' elements')
for i in range(len(z)):
print('Element with index ',i,' = ',z[i])
After running this code, you should see that the list z contains 3 elements. The lists x and y are said to be nested within the list z. Therefore, indexing elements within these lists will require a second index as follows:
where the second index refers to elements within the nested list. To print out all elements on a nested list, we could also use a loop:
for j in range(len(x)):
One very useful feature of Python is its ability to reference elements in loops without the need to reference an index, as this example shows:
for value in z:
In this example, the variable
value iterates across all the values in the list z without the need to create an actual index. This is a very powerful feature in the Python programming language. Practice as many examples as you can. It is important to master indexing before we move forward to slicing.