Each programming language uses and implements lists/arrays in a slightly different way, but as the focus of this course is Python, I’ll show you how Python lists work.
Creating a list is as simple as assigning a variable, albeit with some added syntax. The most important is the square brackets ([]); when you see square brackets in a piece of code, you can be pretty sure that lists or arrays are involved.
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 = ["Jakob", "Caitlyn", "Martin", "Sara"]
You can define mixed lists:
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] ]
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] = "Tea"
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 also 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). You might want to use some of these common methods when working with lists.
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 in 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")
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.