NCEA Compatibility
Module: as91883 Develop a computer program
Requirements: Lists
Go to topModule: as91883 Develop a computer program
Requirements: Lists
Go to topOn completion of this section you will know the nature of lists and how to:
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:
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.
Go to topAbove we have some examples of the concept of list, at least as being used in Python programming. Firstly, like all variables, lists have names. Above our four lists have names: Distances, Attempts, Measures and NumOfPages.
Let us focus on the first list: Distances. Each item of the list are referred to as an element. Distances has eight elements 15, 215, 75, 128, 57, 84, 45 and 162.
An important feature to note about lists is that the elements start counting at zero and thus the elements of the list are numbered 0 to 7. This can be seen in Fig 1 above.
To read any of the list's elements we need to use the name of the list and the index number of the element we want. Thus to get the third element and store it in a variable named dstnc we would a line of code like:
dstnc = Distances[2]
This would result in the value 75 being stored in the variable dstnc.
(Recall that the third element will have an index of 2 since the indexes start counting at zero.)
Click here to download a copy of the exercisesBefore moving on it is strongly suggested that you download a copy of the exercise and, using Fig 1 as an example, complete all of the exercises.
Go to topNow let us look at how we could apply those ideas to a program.
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’.
Fig 1 below shows the results of the different print lines of Listing 1 showing the lstNames in its various configurations.
Go to topThe module shown shown in Listing 2 above is very similar to its predecessor in the page when we were looking at modules. The only difference is that the function showData() now has six parameters instead of the three it had before. In the previous version we were only dealing with one single employee, where the user entered name, hours and rate, after which th system calculated the gross, tax and net and then printed out the last three items.
In the current example we are dealing with an indefinite number of employees. For each employee we shall get the values for the name, hours and rate from the user. From the hours and the rate we can calculate the gross, tax and net. Each one of those values will be appended to a different list, i.e. the name will be appended ot a list that holds the names, the hours appended to a list that holds the hours etc. Only when the user stops entering a new employee will the program move to the lists and start printing the data in them. Thus in order to avoid confusing one employee's pay with another's we shall for each employee print the name, hours, rate as well as the gross tax and net. This is the reason that the function showData() is twice as long here as it was in the previous example.
In this program we are going to process an indefinite number of employees. For each employee we will need the following data: name, hours worked, hourly rate, gross, tax and net. The program is structured in such a way that for each employee the name, hours and rate are collected from the user, then the gross, tax and net are calculated and finally the values of each of the six items mentioned are appended to its own list. Once the user ceases data entry, the details of all of the employees are printed.
At lines 3 to 8 six lists are created and their names indicate which data items will be stored in each. Thus employee names will be stored in lstEmpNm, hours in lstHrs etc.
The rest of the program consists of two loops: a while loop which spans lines 10 to 24 and controls the input, processing and storage of each individual employee's data, and a two line for loop which spans lines 27 and 28 and prints out the details of each employee.
Line 10 contains the priming read for the while loop. It gets the employee's name from the user. The user's input is tested for not being blank at line 11. If this is the case the body of the loop, which consists of lines 12 to 24, is activated.
Lines 12 and 13 get the values of the hours and rate from the user, storing them in floatHours and floatRate respectively.
After this we get to the processing section where the values of the gross, tax and net are calculated at lines 15 to 17 and stored respectively in the variables floatGross, floatTax and floatNet.
By now we have the following data on the employee:
Each value stored in an appropriately named variable. Our next task is to append each of those values to the the appropriate list. This action spans lines 18 to 23.
Now we get to use our six list items.
At line 18, the value stored in the variable strName is appended to the list lstEmpNm. In a similar manner the value of the variable floatHours is appended to the list lstHours. The values stored in the rest of the variables are appended in the same way to the appropriate list. This means that the details of the first employee is stored as the first element of each of the arrays. On its second iteration of the while loop the details of the second employee is stored as the second element of the same lists. This continues until the user finishes data entry by entering a blank value for the employee name.
Line 24 is the main read for the loop and thus the user is once again asked to enter an employee's name. Control is then passed back to line 11 where the value of the variable strName is once again tested for not being blank.
If the user enters a blank at line 24 then the test at line 11 returns false, and thus the body of the loop is skipped and control passes to line 27. This line along with line 28 form the body of a for loop. The purpose of this loop is to print out the same indexed value for each of the six lists defined at lines 4 to 8. Let us look at this in more detail.
When starting the program the lists at lines 4 to 8 are empty. When we enter the loop, then by the time we get to line 17 all the variables strName, floatHours, floatRate, floatGross, floatTax and floatNet have all of the data relating to the first employee stored in them. Lines 18 to 23 store those values in the firs element of each list. On the second iteration of the loop the second employee's details are stored in the second element of each list. This applies to all the other employees that are added. The result of this is that to find the details of the first employee we look at the first element of each of the lists. To find the second employee's details we look at the second element of each list. The same applies to all the other employees. Now let us look at how this applies to our two-line for loop at lines 27 and 28.
The controller for the loop is for intLC in range( len(lstEmpNm ))
. There is only one parameter in the range() function and therefore we know that the value of the loop counter, intLC starts at zero and at each iteration increments by 1 until it reaches the value in the range() function. This value of course is len(lstEmpNm), i.e. the length of the list lstEmpNm. Thus, if we entered details of six employees then the number of elements in lstEmpNm would be six, which also would be the value of the len() function. Thus if we entered details of six employees the upper range of the for loop would be six and the loop counter intLC would iterate through the values 0, 1, 2, 3, 4 and 5. Now looking at line 28, when intLC has a value of 0, the values passed to the function showData() would the first element of lstEmpNm, the first element of lstHrs etc. as far as the first element of the list lstNet. In other words we are passing it the six data elements relating to the first employee. Once the printing function has completed its operation the loop counter intLC increments to 1 and the showData() is called once more, now with the second element of each list as its parameters.
This processing of the elements of the array continues until until intLC reaches the value of len(lstEmpNm).
Fig 4 shows a sample run of the program where details of four employees have been entered.
Go to topWe stated earlier that lists, as well as containing integers, floating point numbers, text values etc, can also contain other lists. If a list exclusively contains only other lists then it is referred to as a two-dimensional list.
Here we shall look at such a list and to do that we shall modify the code in Listing 3. That code put all of the employees individual details into separate one-dimensional lists, i.e names were held in one list, hours worked in another list and so forth. In this example we shall use a single list for holding all employee data, and once an employee list is complete we shall append it to another list. The code is shown below in Listing 4
Listing 4 is quite similar to Listing 3. It uses the same module for calculating the payroll as can be seen at line 2. Also the main body of the code is a while loop which has its priming read at line 6 and its body spanning lines 7 to 24. Also the output section at lines 25 and 26 is simply a series of calls to the the function showData() from the C9_Payroll_Module
The main differences are at the beginning. Instead of the six lists declared in the previous version, only two are declared here. The first at line 3, lstEmployee, will contain all of the values relating to one employee, i.e. name, hours worked etc. The second list, lstOrg, declared at line 4 will contain separate copies of the lstEmployee type.
For the data entry of the first employee the lines 6 to 9 are used, while the lines 11 to 13 are used to calculate the gross, tax and net.
Lines 14 to 19 are used to populate the list lstEmployee with the employee's details.
Line 20 appends a copy of the contents of lstEmployee to the list lstOrg
Lines 21 and 22 display on the monitor the contents of both lstEmployees and lstOrg. Strictly speaking those two lines would not be in a working program but are inserted here so that as you enter details of each employee, you can interactively see the contents of lstEmployee changing for each employee that is entered while lstOrg contains copies or all previous values of lstEmployee.
Fig 4 above shows a sample run through Listing 4. Notice how the employee list only contains the details of the most recently entered employee, while the list lstOrg expands at each run through the program as the list containing the details of the most recent employee are appended to it.
Go to topIf we have a large number of values that are processed in an identical fashion then using lists is the most efficient way to do it.
Lists are stored in memory the same as other variables Lists can have multiple elementsEach time an element is added to a list the the size of the list increases
Each element of a list has its own unique index
As well as adding elements to a list, elements can also be removed.
A list can be appended to another list
A list may contain only other lists
Go to top