# Xtralien Scientific Python: Lists

Lists are a core data structure in Python that are similar to arrays or vectors in other langauges, however they don't have any limitations on length.

A list consists of of a series of any values. Additionally, lists are values themselves, so they can be stored in variables. Lists are mutable, which means they can be modified without needing to create a new copy.

To create a new list you can either use the [] shortcut or use the list() function. Normally, the [] notation is used for clarity.

Lists are good for collecting items as you go through your script. Additionally you can access items inside Lists by using square brackets ([]) after the symbol storing the list. This is called subscript access.

You cannot go out of bounds (try to access outside the list), and attempting to do so will cause an IndexError. The bounds are defined by the length of the list ($n$). Because the indexes in Python start at $0$ (The first item is index 0), the maximum index in a List is $n-1$.

Technical note: For those who are interested, lists are implemented internally as a linked-list data structure. This means that it might be better to use one of numpy's arrays if speed or memory are of the essence.

It is possible to create lists using two different methods.

One of the methods is to use the list function.

a = list()

The other method is to use square brackets.

a = []

Both of these methods produce an empty list and are equivalent. This can be checked printing the result of an equality comparison between the two.

print(list() == [])

This should print True to the console, indicating that they are equivalent.

## Creating a populated list

In the previous example we created a list that was empty. This can be useful if you want to add items as you move through the program, but you may also want to initialise a list with some standard elements. The way that you do this is different for each different type of list creation.

For the list function you need to provide an iterable for the function to turn into a list. This is nominally another list or a tuple, however it can be anything that provides an iterable interface, such as a generator.

a = list((1, 2, 3))

The second method, using the [] notation, is simpler and more direct. You simply insert the elements that you wish to be in the list.

a = [1, 2, 3]

Both of these methods will create identical lists, which would show [1, 2, 3] if inspected.

## Reading items from the list

Reading items from the list is the same as many other languages. In Python it is called subscript access and uses [] after the variable name. Using the example of a = [1, 2, 3, 4] this is shown below.

b = a[0]

This would assign b the value 1. This is because 1 is the first element (index 0) of the list stored at variable a.

It is also possible to get a subset of a list, called a slice in Python, using similar syntax. This is done by using the syntax list_variable[start:end] where start and end are both optional. If start is omitted then the index 0 will be used. Likewise, if end is omitted then the length of the list will be used as an index, due to the exclusive nature of end. An example of this would be as follows.

b = a[1:3]

This would assign b the value [2, 3] as these are the elements at indexes 1 and 2 respectively.

You can add to a list by appending items. This is done by providing the element you wish to add to the append method of the list.

a.append(4)

This will add a 4 to the end of the list. If the list a is inspected it would display [1, 2, 3, 4].

## Removing items

If you need to remove items from a list you can either pop them off of the list or delete them.

If you choose to pop an item from a list you need to use the pop method of the list, providing the index of the item you want to pop from the list. When you pop from a list you get the element you removed.

popped = a.pop(0)

In this case we provided the index 0 (the first element) and the first element was removed and returned to us, stored in the variable popped in this case.

If you don't need the popped value then it may be more economical to use the del keyword. This simply deletes whichever value you point to, removing from lists or dictionaries if needed.

del a[0]

This code will have the same effect on the list a however it won't return the popped value for you, instead removing it completely.

## Troubleshooting

When accessing elements from lists in Python, you will sometimes try to access an element that doesn't exist. If you are getting a single element from a list then you might cause an error to occur. This is an IndexError and it needs to be handled, otherwise your program will terminate.

b = a[10]

The example above would cause an IndexError. There is more information on Errors and Exceptions in a later tutorial.

It is worth noting that if you access a slice that is outside the scope of the list then an empty list is returned and no exception is raised.