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:

rainbow list.png

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:

py109.png

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:

py110.png

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:

py111.png

To print the list use an asterisk - * - before the list name otherwise it will print it with brackets and apostrophes.

list1.png
list2.png

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:

list1.png

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:

py113.png
py114.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:

py115.png

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:

py71.png
py72.png
py69.png
py70.png

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. 

py73.png
py74.png

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:

py75.png
py76.png

Practice Task 3

Create a list of at least five breakfast cereals.

Print each cereal on a separate line.

Example solution:

py125.PNG

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:

py126.png

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:

py127.png
py128.png

Practice Task 4

Ask the user to enter as many words as they can in 20 seconds.

Add each word to a list.

When the 20 seconds is up the user has to enter STOP. You do not need to count, the user should be trusted to type STOP.

Use the len function to print the first and last words entered - do not count STOP as an entered word.

Bonus: Also print the total number of words entered.

Example solution:

list3.png

Sorting Lists

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

list5.png
list4.png

The .sort() command can be used to sort values in descending order by including reverse = True in the brackets.

list6.png
list7.png

Practice Task 5

Create a list of at least six names.

Sort the list.

Print each element on a separate line.

Example solution:

py134.PNG

Searching Through Lists

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

py135.png

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:

py136.PNG
py137.PNG