ComputerScienceUK.
com CSUK:Teache
r
Introduction
So far, when working with data, we have made use of variables, to store single items
of data, when our program is running.
However, programming languages also offer a way to store a collection of data items,
together, under a single identifier. This is achieved with data structures. And we will
now learn about a particular type of data structure, known as a list.
Lists
In python, we declare a list in the following way:
The data items are separated by comas and contained inside square brackets.
This is then assigned to an identifier.
A list can be visualised as a table of data, where various data items are held together.
Index 0 1 2 3 4
Item car bus train plane boat
An important point to remember is that items are indexed (given a position value).
Furthermore, this system begins with index zero! That means the first position of the
list is position 0!
Accessing List Items
For the given list above, if we wished to access a particular item, we would do so
using it’s index.
For example:
transport[0], will
access the item
at index zero
(position 1),
which is ‘car’.
transport[3],
will access the
item at index 3
(position 4),
which is ‘plane’.
Adding a new List Items
Computer Science UK Membership Site Licence: Do not share outside of your centre 1
[Link] CSUK:Teache
r
For the given list above, if we wished to add a new list item, we would use the
append() method.
For example:
Here, you can see how the new item ‘bicycle’ has been appended (added) to the end
of the list.
List Manipulation Methods
In fact, once we have created a list there are lots of things we can do with it.
For example, we can:
Reverse the order of the list
Sort the list into alphabetical order or numerical order (if the data is number)
Remove items from the list
Find the position of certain items on our list
Count how many times a particular item appears in the list
Insert items into a particular position of the list
“Pop” an item out of the list.
Let’s take a look at each of these methods in a little more detail…
Reversing the List’s Order
We can easily reverse the order of a list by using the reverse() method:
Sorting a List into Alphabetical Order
The sort() method can allow us to order a list into alphabetical order:
Removing an Item from a List
Computer Science UK Membership Site Licence: Do not share outside of your centre 2
[Link] CSUK:Teache
r
Using the remove() method, along with the item, we can remove an item from a list:
Locating an Item’s Index (Location)
If we need to locate an item in a list, we can use the index() method, along with the
item, and this will return the item’s index:
Counting List Items
It may be that we need to find how many times a particular item occurs in a list. Using
the count() method, along with the item, will return the number of times the item
appears a list:
Inserting into a List
In addition to appending an item onto the end of a list, we can insert an item into a
list, at a given location. Using the insert() method, along with an index and item, we
can insert the item at a given index:
Computer Science UK Membership Site Licence: Do not share outside of your centre 3
[Link] CSUK:Teache
r
Popping Items from a List
The pop() method will remove an item from a list and return the item that has been
popped.
Application of a 1D List - An Example
Often, a program will use a list to record user inputs.
In Python we need to create an empty list and then use the append function to add
new items into the list.
We could do this in a loop to be more efficient:
Computer Science UK Membership Site Licence: Do not share outside of your centre 4
[Link] CSUK:Teache
r
List Dimensions
We have just seen how lists allow us to store more than one item of data under the
same identifier. But actually, lists themselves can also contain lists. A list containing
lists is known as 2D lists…
…and we can create 2 dimensional lists with ease!
1D Lists in Python…a reminder
As we have already seen, a 1-dimensional list can be coded in python like this:
List = [23, 3, 1, 4]
Where:
List[0] = 23
List[1] = 3
List[2] = 1
List[3] = 4
2D Lists in Python
However, a 2-dimensional list is coded in python like this:
List = [ [23, 3, 1, 4] , [5, 22, 6, 3] ]
Where:
List[0] = [23, 3, 1, 4]
List[1] = [5, 22, 6, 3]
List[0][0] = 23
List[0][3] = 4
List[1][1] = 22
List[1][2] = 6
Visualising 2D Lists and Accessing 2D List Items
Computer Science UK Membership Site Licence: Do not share outside of your centre 5
[Link] CSUK:Teache
r
Here is a visual of two_dim_list, along with a demonstration of how each list item can
be accessed:
You can visualise a 2D list as a 2D table, where each sub-list is a row.
PRIMM TASKS
1: Predict
Computer Science UK Membership Site Licence: Do not share outside of your centre 6
[Link] CSUK:Teache
r
Code
What do you think the code will do?
Write the program’s output exactly as you think it will appear.
Predictio
>>>
ns
2: Run
Now type in the code and run the program.
Show a screenshot of the actual output below.
Was it the same as your Yes No
prediction?
Were there differences? If so, show or describe these below.
3: Investigate
What happens if the code is
altered to the following?
Computer Science UK Membership Site Licence: Do not share outside of your centre 7
[Link] CSUK:Teache
r
Explain the result.
4: Modify
Modify the original
code so that it prints
out the 2D list, with
each sublist sorted
into ascending order.
Place a screen shot of
your code opposite
and explain what the
code is doing.
5: Make
Create a program in python for each point below and add a screenshot of your code
in the boxes provided:
1D Lists Tasks:
1. Create a list in Python containing your top 10 films and then display the list on
the screen.
2. Reverse the order of the list and then display it onto the screen.
3. Sort the list into alphabetical order and then display the list on the screen.
Computer Science UK Membership Site Licence: Do not share outside of your centre 8
[Link] CSUK:Teache
r
4. Add an eleventh favourite film to the end of your list and then display it on the
screen.
5. Remove a film (that you no longer feel deserves its place on the list) and then
display the updated list onto the screen.
6. Find the position of certain film on your list and then program it to display a
message which states that the film is located at position ? of your list.
7. Use the ‘insert’ function to insert a new favourite film into a particular position
of the list.
Computer Science UK Membership Site Licence: Do not share outside of your centre 9
[Link] CSUK:Teache
r
8. “Pop” out each item out of your list in turn and each time display the film which
has been popped out. Do this until your list contains nothing.
2D Lists Tasks:
1. Create a 2D list that contains your 5 favourite films, along with your rating for
that film, out of 10. For example films = [["Back to the Future", "10"],["Back to the
Future 2", "9"],...]
2. Using the completed 2D list from the previous task, write the code that will print
the 3rd film in your list and its score.
3. Using the completed 2D list from the previous task, write the code that will print
the 4th film's score.
Computer Science UK Membership Site Licence: Do not share outside of your centre 10
[Link] CSUK:Teache
r
4. Create a FOR loop that will loop through the 2D list, printing out each sublist
(film and score) onto its own line.
5. Create a FOR loop that will loop through the 2D list, printing out only the name
of each film, on its own line.
6. Append a new film and score to the 2D list.
7. Use the sort() function to sort the list into alphabetical order.
8. Use the index() function to locate the area of a film of your choice.
Computer Science UK Membership Site Licence: Do not share outside of your centre 11
[Link] CSUK:Teache
r
9. Create a program that uses a FOR loop to asks the user to enter their own
favourite films (top 5) and their ratings, which then adds this information into a 2D
list. The program should then output this list once the user has finished entering
their top films.
Computer Science UK Membership Site Licence: Do not share outside of your centre 12