Introducing lists
Share this post
Last week we looked at the role of functions in programming, and you learnt how to define and call your own functions with both parameters and return values. These are going to be really useful as you begin to implement your own algorithms.
Another programming technique that will be useful for algorithm writing is the use of lists to store data, as many algorithms involve manipulating data. Over the next few steps, we’re going explore what lists are and how they work.
What is a list?
A list (also called an array) is a tool for storing data, just like a variable. However, whereas a variable can store one piece of data at a time in a named location, a list can store many.
Like a variable, a list is a named storage area within the computer’s memory. If we imagine the computer’s memory like a bookshelf, then each variable is like a named box on that shelf.
Every time your program refers to a variable by name, the computer looks at the contents of the corresponding box. A list works in similar way, except that instead of being a box to hold one piece of data, it can be split up into any number of smaller sections, with a piece of data in each. Each section is then assigned a number or index, starting at 0, to allow it to be referred to. So in a list called players
, to refer to the third item from the start, you might write players[2]
(remember the first index is numbered 0).
Each programming language uses and implements lists/arrays in a slightly different way, but as our focus is Python, let’s look at how Python lists work.
Creating lists
Creating a list is as simple as assigning a variable, albeit with some added syntax. A Python list can be written as a comma-separated list of values, contained within square brackets. So a simple list of integers can be created like this:
scores = [6,2,9,7,4,6,3]
A list of strings:
names = ["James", "Caitlyn", "Martin", "Laura"]
You can define mixed lists:
~~~ python
profile = [“James”, “January”, 21, “Tea”]
~~~
Empty lists:
shopping_list = []
Or even lists of lists:
magic_square = [ [2, 7, 6], [9, 5, 1], [4, 3, 8] ]
Accessing items
Once you’ve stored items in a list, you’re probably going to need to refer to them again later. This is done using the list name followed by a pair of square brackets containing the index (or indexes) you want to refer to.
So in order to refer to the first item in a list, you would write:
scores[0]
To replace the third item in a list, you could write:
names[2] = "Marc"
You can refer to a list item by its distance from the end of the list instead of the start. To talk about the last item in a list you would use [-1], for the second-to-last [-2], and so on:
profile[-1] = "Cake"
You can even refer to a whole subsection or slice of a list, using a pair of indices separated by a colon:
scores[1:3]
In Python, a list is what we call an object, which means it has lots of built in actions, called methods. (To find out more about objects and methods, you can take our Object-Oriented Programming course). Let’s take a look at some common methods you might need when working with lists.
Adding items
There are two important methods for adding new values to a list. Quite often you will simply want to add or append a new item to the end of a list. However on other occasions you may want to insert a value at a specific place a list.
To append an item, use the list name followed by the method .append
and the value you want to add:
shopping_list.append("bananas")
To insert an item, as you can probably guess, you need the .insert
method. In this case you need to specify the value you’re inserting and the position where it should be inserted.
names.insert(1,"Dan")
Removing items
Removal of items is similar to insertion, and again there are at least two ways to do it. You can remove or pop an item from a particular position, using its index, and you can also remove it by searching for its value in the list.
To pop the item that’s in a specific position, you simply use the .pop method with the index number:
scores.pop(3)
This would remove item 3 (the fourth item) from the list. If no index number is given, the last item in the list is popped.
In some situations you may instead want to remove a specific value. You can do this with the .remove
method:
scores.remove(3)
This would remove the first occurrence of the value 3
, whatever position it was in.
There are many more ways of manipulating lists; these are just the most common. In the following steps, you’ll get to use and apply these ideas through some short programming activities.
Discussion
In this step we’ve covered a lot of ideas, lots of which will be new to you.
- Use the comments below to discuss these and confirm your understanding.
- What other examples of data might you store in a list?
- When might you introduce lists to your learners, and how would you do so?
Share this post
Programming 102: Think Like a Computer Scientist

Programming 102: Think Like a Computer Scientist

Reach your personal and professional goals
Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.
Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free