Python 8a - Using Lists

Lists

A list is a series of data in order

 

Data can be added to and removed from lists so they can change in 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 rainbow colours in order would start at 0 like this:

Set Up & Add to a List

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:

Now for a specific example for a geography app.

If you wanted to create a list with some data elements already inside, then you just need to add them within the square brackets and separate each one with a comma, such as:

To add a new entry to the end of a list 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.

Practice Task 1

Create a list named bands and include three of your favourite musicians.

Use the .append command to add two more bands to your list.

Print the list by writing print(bands)

Example solution:

Remove Data from a List

There are two main ways of removing data from a list.

To delete data in a specific position in your list 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:

Practice Task 2

Create a list with five elements and print it.

Remove the first and last elements of the list and print it again.

Example solution:

Printing Lists

Type the list name into a print command to output the complete list. Typing an asterisk * before the list name removes punctuation:

To print a list line-by-line use a for loop to cycle through each item.

 

'city' is just a variable name and can be replaced by anything relevant to the context, such as 'colour' in a list of colours or 'names' in a list of people. 

To print the data elements on the same line then you can use the end command which prevents Python from creating a new line and instead states what should go after each entry.

 

For example, end = “,  “  adds a comma and space between each element:

Practice Task 3

Create a list of at least five breakfast cereals.

Print each cereal on a separate line.

Example solution:

Finding the Length

To find the length of a list 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 print or delete the last value in a 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:

Practice Task 4

Create a list of at least 8 short words.

Use the len and .pop functions to remove the second last element.

Example solution:

Sorting Lists

The .sort() command will sort elements in a list into alphabetical order (if a string) or numerical order (if a number).

Practice Task 5

Create a list of at least six names.

Sort the list.

Print each element on a separate line.

Example solution:

Searching Through Lists

An if statement can be used to see if a certain word appears within a list.

Practice Task 6

Use the Pokemon example above to help you.

Create a list of five of your friends.

Create an input line to ask a user to enter a name.

Use an if statement to check if their name is in the list.

Print appropriate responses if it is found and if it isn't.

Example solution:

© CSNewbs 2020