Computer Science GCSE
Lists
Lists, also called Arrays, are a collection data type. That means that one variable name is used to store more than one data item - by creating a list of items.
Lists are incredibly useful data types and will almost certainly be essential for controlled assessment pieces. They might well also occur on exams - where they will be called arrays. A list is the way in which Python implements the idea of an array.
It's possible to add items to a list, remove them and sort them. They are also incredibly useful when you need to return more than one item from a function.
Note that strings behave very similarly to lists. In fact a string is basically a list of characters - just using a slightly different notation. There's more on strings at the bottom of this page.
Setting up and using Lists
Lists are sequences of data items. In Python we use square brackets [ ... ] to signify a list. Items within the list are divided by commas. Lists can be initialised like this:
lotteryBallsList = [17, 12, 45, 9, 1, 6]
emptyList = [ ] # create an empty list
Once a list is created we can access individual items in it using the inde number of the item. This is a number which refers to which item is which in the list. For example:
fruitList[2] # refers to the item "Kiwi Fruit"
IMPORTANT: index numbers start from 0.
This means that the first item in the list is index 0. The second item is index 1 etc... The last item in the list has an index of the length of the list - 1. This all has implications when you're using loops to iterate over a list, for example.
print(fruitList[0]) # prints "Apples"
print(fruitList[1]) # prints "Bananas"
print(fruitList[3]) # prints "Cherries"
print(fruitList[4]) # an index out of range error tells you this index doesn't exist in the list
The built in function len will give you the length of a list - i.e. how many items are in the list. This is helpful when using loops of any kind.
len(fruitList) # returns 4 - the number of items in the list
The pages on for loops, while loops, file handling and lists of lists contain a lot more uses of lists.
Shopping list exercises
A shopping list is a good example of a list. These examples should help develop an understanding of lists and how they work. More reference material is available at the bottom of this page.
Shopping List Exercise 1.0 - a fairly simple guide to setting up and using a shopping list
Shopping List Exercise 2.0 - a less structured guide to setting up and using a shopping list for people with more confidence using lists
An implementation of the 1.0 exercise is shown below. There are, of course, other ways to code this sort of thing.
print()
shopping = [ ] # create an empty list
item = "" # create a blank item
while item.upper() != "XXX":
item = input("Enter an item to add (XXX to exit): ")
shopping.append(item) # adds item to list
print()
print(shopping) # print list in one go
print()
print("Items in list: " + str(len(shopping)))
It's essential to create an empty list first. You can't use the .append() method to add to the list without there being a list there to beging with.
It's also crucial to have item set as "" to start with. Otherwise the while loop condition fails as Python won't know what item is supposed to be.
To print the items one per line you'd use a for loop:
for i in range(0, len(shopping)):
print(shopping[i])
You'd add this in place of the line where the list is printed all in one go.
The only problem with this code is the XXX is added to the end of the list. To get around that you can add a conditional statement inside the while loop:
shopping.append(item) # adds item to list
This keeps XXX out of the final list, which is probably what you want. It's details like this that turn an OK program into a good one.
Useful bits and bobs
Basic help and other exercises. The list methods are particularly import
Arrays - a basic introduction
Python list methods - methods to use with lists
Hero inventory - a walk through developing skills with lists - using an inventory for a role playing game
Menu system - a way of creating a menu system with lists involved as well (now with the missing colon found and added)
Strings are essentially lists and behave very similarly.
Strings intro - what a String is and how to use it
String methods - summary of string methods