On completion of this section you will know the nature of lists and how to:
· Access individual items of the list
· Add an extra item to the list either at the end or in the middle
· Add the contents of another list
· Delete items from the list
Up to now in our payroll example we have had a number of
variables dealing with different aspects of the payroll. Thus we had separate variables for the hours
worked, the hourly rate, gross, tax and net. This was the best way to handle
the processing on that occasion because of the following reasons:
·
The number of variables was small
·
Each variable was processed differently from all
of the others
On the other hand if we had names of twenty items and each item
was to be processed in exactly the same way then having a different variable
for each item would be a very inefficient way of programming. In a situation like this we would use a list.
If we have a list of five String values describing various
professions and if every item in the list is to be processed in exactly the
same way then creating five separate variables would be a very inefficient way
of processing them. The best way is to
create a list as shown below
|
0 |
Carpenter |
|
1 |
Plumber |
|
2 |
Roofer |
|
3 |
Drain Layer |
|
4 |
Electrician |
listSkills=['Carpenter','Plumber','Roofer','Drain Layer','Electrician'];
Logically we can view this as a variable listSkills
which has five indexes as follows: listSkills[0], listSkills[1],
listSkills[2], listSkills[3],
listSkills[4]. Notice that the list indexes starts at zero
and therefore the highest index of this five element list is 4. Each of the
five names are stored in one element of the list. Thus listSkills[2] shows the third element of the array,
i.e. ‘Roofer’. A graphic version of the array is shown at
the left.
|
1 |
#C6Lists Start Add |
|
2 |
lstNames=['Tom','Dick','Harry'] |
|
3 |
print("This is the
original list") |
|
4 |
print(lstNames) |
|
5 |
lstNames.append('Mary') |
|
6 |
print("This is the
list after 'Mary' has been added") |
|
7 |
print(lstNames) |
|
8 |
lstMoreNames=['Anne','Kate'] |
|
9 |
lstNames.extend(lstMoreNames) |
|
10 |
print("This is the
list after another list has been appended") |
|
11 |
print(lstNames) |
|
12 |
lstNames.insert(3,'Tom') |
|
13 |
print("This is the
list after 'Tom' has been added in the fourth position") |
|
14 |
print(lstNames) |
|
15 |
lstNames.pop(0) |
|
16 |
print("This is the
list after the first item has been removed") |
|
17 |
print(lstNames) |
|
18 |
print(lstNames[0]) |
|
19 |
print(lstNames[1]) |
|
20 |
print(lstNames[2]) |
|
21 |
print(lstNames[3]) |
|
22 |
print(lstNames[4]) |
|
23 |
print(lstNames[5]) |
Listing 1
The code example above does nothing useful apart from demonstrating some of the methods that we can use to process lists.
Line 2 creates a list named lstNames with three elements in it, ‘Tom’, ‘Dick’ and ‘Harry’.
Line 5 uses the append method to add an extra element to the end of the list i.e. ‘Mary’. The list lstNames will now contain the elements ‘Tom’, ‘Dick’, ‘Harry’ and ‘Mary’.
Line 8 creates another list named lstMoreNames containing two elements ‘Anne’ and ‘Kate’.
Line 9 uses the extend method to add the elements of the second list to the first one. On completion of this line lstNames will contain the elements ‘Tom’, ‘Dick’, ‘Harry’, ‘Mary’, ‘Anne’ and ‘Kate’.
Line 12 uses the insert method to add the element ‘Tom’ to the list at position 4. The list will now contain the elements ‘Tom’, ‘Dick’, ‘Harry’, ‘Tom’, ‘Mary’, ‘Anne’ and ‘Kate’.
Line 15 uses the pop method to remove the first element,
i.e. element zero, from the list thus leaving the list as ‘Dick’, ‘Harry’, ‘Tom’, ‘Mary’, ‘Anne’ and ‘Kate’.
Figure 1 below shows the results of the different print lines of Listing 1 showing the lstNames in its various configurations.

Figure 1
A list can contain any number of elements. Each element is identified by its position in the list. As well as creating a list we can use a number of methods to process the list. Among those methods are:
1. append, which adds a new element to the end
2. extend, which adds the contents of another list to the end
3. insert, which adds an element into the middle of the list. The first argument of this method indicates where in the list the new element is to be positioned
4. pop, which removes an element from the list. The argument indicates the index of the element which is to be removed.
Copy Listing 1 and run it. Check that the output is similar to that shown in Figure 1.
Next play around with the code by modifying the number of the elements in the list and by using different combinations of the append, extend, insert and pop methods. Ensure that after applying each method you use the print function to display the list in its current format.