Skip to 0 minutes and 3 secondsAs many algorithms involve the manipulation of sets of data, another programming technique that will be useful to our algorithm writing is the use of lists. A list, sometimes called an array, is a tool for storing data, just like a variable. However, unlike a variable, which can only store one piece of data, 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.

Skip to 0 minutes and 38 secondsEvery time your program refers to a variable by name, the computer looks at the contents of that box. A list works in a similar way except that, instead of being a box with one area to store data, it can be split into any number of smaller sections. Each section is then assigned a number or index, starting at 0, to allow it to be described. To retrieve the fourth item, we would add the number 3 to the end of the name of the list.

Skip to 1 minute and 14 secondsEach programming language uses and implements lists and arrays in slightly different ways. However, our focus is Python. So let's look at how it does it. Creating a list in Python is as easy as typing the name of the list you want to create, equals, and then a pair of square brackets. The list we have here is currently empty. And that's very useful if you want to build that list up throughout the program. However, what we're going to do here is-- I'm just going to make a really quick list with a whole range of numbers in it.

Skip to 1 minute and 49 secondsOK, now if I run my program-- if I type print nums, then I get my whole list out. And I can refer to each element in the list by its position or index. So I'm going to do some square brackets on the end. And I'm going to ask for element or position 1. If I print that, many of my students might expect me to get the answer 4, because they look at the list and think it's the first item. But actually, Python counts differently. Number 1 or position 1-- element 1-- is actually this one here. Python numbers its position starting at 0. So this is position 0, position 1, position 2.

Skip to 2 minutes and 37 secondsWe could also, if we want, add numbers to our list. So we can do nums.append. And if I put a number in here and I run my program and then I print nums, then I can see that I've appended a number 2 to the end. If I do nums.pop, this is going to remove an element from my list. If I give it without any parameters, it will simply remove the last element that I added. So if I print nums then go, then I have added the number 2 to the end, and then I've popped it.

Skip to 3 minutes and 25 secondsHowever, if I put a position in there-- so if I wanted to get rid of this number 7, which is position 0, 1, 2, 3, 4-- if I pop position 4, then when I print nums, then I can see that that 7, which was in position 4, has now been popped. And the 2 that I added has not been popped. So pop is for removing. Append is for adding. And the other way of adding things into my list is with the Insert method. So if I insert, I first of all say where it's going to go-- so that's my position. And then I give it the value that I want to insert. So let's insert 100 after position 3.

Skip to 4 minutes and 17 secondsAnd I'm also just going to add print nums to the end of my program to save me typing every time. If I stop and I run, then I can see that it went to position 3 and then inserted the number 100. And finally, the last method that I'm going to show you is something where we can remove. So if I said Remove-- this does not remove by position. This removes based upon the value that I type in. So I tell it which value-- what the value is-- that I want to remove. So here, if I wanted to remove the value 90 from my list, I wouldn't give it a position reference. I would type in the number itself.

Skip to 4 minutes and 59 secondsIf I stop my program, run it, I can see that 90 has disappeared. And there are a few other ways that we can play around with lists as well. I'm going to show you a couple of things, so we can do-- if we did minus 1 as our list reference, we would actually get the final item in the list. So that count backwards from this end. And we can also do something called List Slicing, which is where, if I said I wanted elements 4 to 7, it would give me element 4, 5, 6, but not 7. It would stop at 7. So there's element 4, 5, and 6.

Skip to 5 minutes and 36 secondsSo there's a few different methods that you can play around with when you're playing with lists. But also, just to show you a couple of examples-- our lists don't have to be numerical. We could have lists of strings. So here's a shopping list with some strings in it. And we can also have lists of lists. So here we have a list with five elements, each of which is a list in itself. And this is really useful if you want to build up two-dimensional arrays or three-dimensional arrays of data. There are many more ways of manipulating lists. These are just a few of the most common methods or functions.

Skip to 6 minutes and 13 secondsIn the following steps, you'll get to use and apply these ideas through some short programming activities. In this step, we've covered a lot of new ideas, many of which will be new to you or your learners. Have a think about when you might introduce these concepts to your learners and how. Use the comments below to discuss these thoughts and confirm your understanding. See you in the next step.

Introducing lists

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.

A set of three shelves with boxes on each shelf. These boxes are red, green, yellow, blue and purple, and are labelled with black text. Some example labels are "name", "a", "b", "score", "time", "isAlive". One long blue box is labelled "highscores".

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).

A box labelled highscores, split into ten sections containing the numbers 91, 34, 65, 77, 44, 22, 52, 58, 12 and 3 in red. Below the box each section is labelled in black, from 1 to 9.

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 video:

This video is from the free online course:

Programming 102: Think Like a Computer Scientist

Raspberry Pi Foundation