# Lists

You might recall earlier we talked about two ‘types’ of data in a program – ‘strings’ and ‘numbers’.   We learnt that you can tell something is a string because it has quotation marks around it.  In this tutorial we are going to meet a new type called lists.   You can’t program a computer game without knowing about lists.  You can tell something is a list because it has square brackets around it -> [].  The truth is I’ve snuck a list in before, so this is not a first meeting.  Do you remember?

In the for/range tutorial we used range(5).   It gave this output:

```>>> range(5)
[0, 1, 2, 3, 4]
```

The object [0, 1, 2, 3, 4] is a list.

To adopt the analogy we used for variables, lists are like a big bucket holding a heap of smaller buckets, although the smaller buckets don’t have names, they just have numbers.  Lists are a very powerful way of allowing you to handle a heap of things in a consistent and general way without having to deal with each of them specifically.

For example, imagine you have two toys.   Let’s say toy0 is an action figure, and toy1 is a scooter.  When you want to store these you would do an assignment like this:

```>>> toy0='action figure'
>>> toy1='scooter'
>>>```

This stores the toys, but it does so in a way which is difficult to deal with in a program.  We will see why in a moment.   After you have made these assignments you do some other things in the program and you get to a point where you want to do different things depending on what toys you have.  You’d have to code something like this:

```>>> toy0='action figure'
>>> toy1='scooter'```

#do a heap of other stuff, which we’re not detailing here!

```>>> if toy0 == 'scooter' or toy1 == 'scooter':
...    print 'We can scoot!'
...
We can scoot!
```

So after doing stuff in the program we come to a decision point with the if statement.   In order to code the  decision point we need to know ahead of time what the names of the individual variables holding the toys are.  Then you can get the computer to check each of the variables to see if either of them is a scooter.  If it is, then it tells us we can scoot.

Your scooter is a little old and you were getting a little bit big for it, so you swap it with your friend at school for a pair of binoculars, so toy1 needs to change:

```>>> toy1 = 'pair of binoculars'
>>> print toy0+', '+ toy1
action figure, pair of binoculars
```

Then, long lost Auntie Annona sends you a late birthday present – a new scooter.  You can’t put in in the toy1 bucket because you’re already storing your binoculars there.  Instead you store it in a new variable toy2 (since there was already something in toy0 and toy1 that you wanted to keep), not where it originally was.   So

```>>> toy2 = 'scooter'
>>> print 'my toys are: '+toy0+', '+toy1+', '+toy2+'.'
my toys are: action figure, pair of binoculars, scooter.
>>>```

However, since you’ve stored it in a new place your program won’t work properly anymore:

```>>> if toy0 == 'scooter' or toy1 == 'scooter':
...    print 'We can scoot!'
...
>>>```

Despite having a scooter (and therefore you are scoot-enabled), the program fails to tell you so because it is looking in the wrong place to find it.  The problem here is you won’t know when you write the program how your toys will change over time, so you can’t write the program to anticipate the different changes of toys which may occur – unless, that is, you do the rest of this tute and learn about lists.

#### Toys as  a List

Let’s implement the same program as a list:

```>>> mytoys = ['action figure','scooter']
>>> mytoys[0]
'action figure'
>>> mytoys[1]
'scooter'
>>>```

Instead of storing the toys in different variables, a list stores them in different places within the same (list) object.  The first place in a list is (somewhat misleadingly) called number 0.  The second place is number 1, the third number 2 and so on.   Don’t ask why, just accept it.  It’s like some people saying the week starts on a Sunday, and others saying it starts on a Monday.  Here, mytoys[0] (note the square brackets) says ‘show me slot zero of the list called mytoys‘.

We can implement the same code as above and get the same sort of outcome:

```>>> if mytoys[0] == 'scooter' or mytoys[1] == 'scooter':
...    print 'We can scoot!'
...
We can scoot!
>>>
```

Notice how toy0 and toy1 (separate variables) are now mytoys[0] and mytoys[1] (list items). When we swap the scooter for binoculars we can do it like this:

```>>> mytoys[1]='pair of binoculars'
>>> mytoys[1]
'pair of binoculars'
>>>```

Now, when we get the new scooter from long lost Auntie Annona, we can use one of the clever things about lists: we can just add something to the end of it – we don’t need to add another variable or name it.  In particular, above, in order to call our new variable toy2, we needed to know that the previous one was called toy1.  This is not necessary for lists:

>>> mytoys = mytoys + [‘scooter’]
>>> print ‘my toys are: ‘+mytoys[0]+’, ‘+mytoys[1]+’, ‘+mytoys[2]+’.’
my toys are: action figure, pair of binoculars, scooter.
>>>

Note: we are adding [‘scooter’] (that is, a list [] containing a single item ‘scooter’) not ‘scooter’ (a string) – adding ‘scooter’ will give a type exception (try it and see).

Unfortunately, our conditional still fails with the list:

```>>> if mytoys[0] == 'scooter' or mytoys[1] == 'scooter':
...    print 'We can scoot!'
...
>>>
```

However, if fails because we haven’t been using any list-Fu (ie any special list skills – probably because we haven’t learnt any yet) not because there’s something wrong with lists.  Let’s revisit the earlier code.

One of the really powerful things about lists is you can leave it up to the computer to remember how many things are stored in the list.   So, while I’ve explicitly referenced mytoys[0] and mytoys[1] here,  I could just have easily said simply mytoys and the computer will work out what is in the list:

```>>> mytoys = ['action figure','scooter']

>>> mytoys
['action figure', 'scooter']```

This turns out to be very important.   Pay attention to the square brackets here, it says this is a list.  Each of the items in the list is separated by a comma ( , <- ie this thing).

Next, lists have a concept of containment.  You can ask the computer to just check whether something is in the list or not, without caring where in the list it is.  So we can rewrite the code like this:

```>>> if 'scooter' in mytoys:
...    print 'We can scoot!'
...
We can scoot!
>>>```

Wow! Did you see that?  This way of programming means it doesn’t matter how many items are in the list, or where in the list your toy is, this piece of code will always work to find whether it is in the list.

When we swap the scooter for the binoculars we could use our special list skills to do it without knowing where in the list the scooter was, but we won’t for now (try >>> help(list) and the remove method if you’re interested).

```>>> mytoys[1]='pair of binoculars'
>>> mytoys[1]
'pair of binoculars'
>>>
```

Now add the new scooter again:

```>>> mytoys = mytoys + ['scooter']
>>> mytoys
['action figure', 'pair of binoculars', 'scooter']
>>>
```

See how we can print the whole list out without knowing how many entries it has.  Up above we needed to know ahead of time that the variables were called toy0, toy1 and toy2 to print them all out.

Now test whether we can scoot:

```>>> if 'scooter' in mytoys:
...    print 'We can scoot!'
...
We can scoot!
>>>
```

W00t! We can scoot!  This would still work if you received a heap of presents and then got your scooter, so that scooter was at the end of (or hidden in the middle of) a very long list.

Lists (and their use as iterators – something we will look at later) are super important in practically all graphical games. If you can imagine writing something like a space invaders game, you would keep all of the aliens in a list.  As they are shot, you would remove them from the list and as more arrived, you would add them.

#### Homework

Make up some of your own lists and test whether particular items are in them or not.  Build a list by putting square brackets around a list of entries separated by commas.  If the entries are not numbers, put inverted commas around them to make them strings.  When testing whether a string is in or out, remember to put it in quotes:

```>>> numberList = [1,2,3,5,45,345,6]
>>> 345 in numberList
True
>>> 7 in numberList

False
>>> stringList = ['a','this is a string','ho hum','baloney']
>>> 'baloney' in stringList
True
>>> baloney in stringList
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'baloney' is not defined
>>> 'palaver' in stringList
False```

Also try printing your list and printing specific entries:

```>>> numberList
[1, 2, 3, 5, 45, 345, 6]
>>> numberList[0]
1
>>> numberList[4]
45
>>> stringList[2]
'ho hum'```

### 4 Responses to Lists

1. lokeyokeyee says:

i love phyton