Using Lists

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:

teletubbieslist

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:

list1

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:

list2


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:

list3

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:

list4.PNGlist4.PNG

 

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:

list5.PNG


Printing variables from a list

A quick way to print a whole list is to write the list name in a print command:

list6

This would output:

list7

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:

list8.PNG

If you ran this program then the output would be the following:

list9

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:

list16

Remember to include a comma after the count variable (i); the code above would give:

list10

Or writing, end = “, “ would add a comma and a space between each variable as so:

list15

list11


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:

list12

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:

list13

If you run the above code then it would give the following output, without the last entry which has been worked out and deleted:

list14


Sorting Lists

The .sort() command will sort elements in a list into alphabetical order (if a string) or numerical order (if a number).
screenshot-repl-it-2016-12-24-11-13-20

This will output:

screenshot-repl-it-2016-12-24-11-14-09


Looking inside a list

You might want to look inside of a list to see if a certain value appears inside it.

pynewbs1


Brief Re-cap

Create a list:
(empty list:)
list = [] (list with data:)
list = [“Hello”, “Goodbye”]

Add data to a list:
list.append(“Geronimo!”)

Delete data from a list:
list.pop(3)
list.remove(“Geronimo!”)

Print data from a list:
(To print a list as it is:)
print(list)
(To print a list line-by-line:)
for i in list:
print(i)

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)

    < Back to Section Eight