Creating a new list
A list is a series of data in order. Data can be added to and removed from lists so they can change size (unlike an array which is fixed and not used in Python).
It is important to note that each data element in a list has an index so that it can be specifically referenced (to delete it for example) and that indexes start at 0. A list of the Teletubbies would start at 0 like this:
To define a list and its contents you need to declare the list name and use square brackets to hold its values. An empty list could be defined as below:
If you wanted to create a list with some variables already inside then you just need to add them within the square brackets and separate each one with a comma; such as:
Adding data to a list
To add a new entry to the end of a list you use the .append() command. Write .append() after the name of your list, with the new data in brackets. For example:
In the example above, “Wellington” would be added to the end of the list.
Removing data from a list
There are two main ways or removing data from a list:
To delete data in a certain position in your list then use the .pop() command, with the position in the brackets. For example:
In the above example “Sao Paulo” would be removed from the list because it is second in the list (remember 0 is first and 1 is second in Python).
Alternatively if you want to delete data with a certain value use the .remove() command, with the value in brackets. For example:
Printing variables from a list
A quick way to print a whole list is to write the list name in a print command:
This would output:
To print a list line-by-line you can use a for loop (you do not need to write range because the range will be the length of the list), such as:
If you ran this program then the output would be the following:
If you wanted to have the data elements on the same line then you can use the end command which prevents Python from creating a new line after each entry and instead states what should go after each entry. For example , end = “” would leave a space between each variable:
Remember to include a comma after the count variable (i); the code above would give:
Or writing, end = “, “ would add a comma and a space between each variable as so:
Finding the length of a list
Sometimes you might want to find out how many data elements there are within a list. For example, you might want to find the length of a list if you have a bunch of integers and you want to calculate an average. You might also want to print or delete the last variable of the list but you may not know what it is called or what its position is.
To find the length of a list you use the len function. You must put the name of the list inside brackets after the len command and save the answer into a variable. For example:
If you printed the length in the example above, by writing print(length) then the program would output: 4
If you wanted to delete or print the last value in your list then you would need to change the value of the length variable by minus 1 (remember that if a list has 8 entries then the last one will have the index 7, not 8). For example, to pop the final entry of a list you could write the following code:
If you run the above code then it would give the following output, without the last entry which has been worked out and deleted:
The .sort() command will sort elements in a list into alphabetical order (if a string) or numerical order (if a number).
This will output:
Looking inside a list
You might want to look inside of a list to see if a certain value appears inside it.
Create a list:
list =  (list with data:)
list = [“Hello”, “Goodbye”]
Add data to a list:
Delete data from a list:
Print data from a list:
(To print a list as it is:)
(To print a list line-by-line:)
for i in list:
Find the length of a list:
length = len(list)
Lists not working? Tick off these common errors:
- Remember to use square brackets when using lists.
- Make sure that you have written your list name variable correctly with each instance.
- When using commands such as .append, .pop or .remove make sure that you put the name of the list before the full stop and you put the correct information in the brackets. Such as: list.append(“Hello”) or list.remove(“Goodbye”)
- Make sure that the value you enter in a .pop command is not larger than the length of the list. For example, writing list.pop(8) in a list of only 8 variables will not work (remember Python’s rule of starting at 0!).
- Remember you must save the length value into a variable, you cannot have the len function by itself. Use an appropriately name variable such as length. E.g. length = len(list)