Python 8a - Using 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:
To print the list use an asterisk - * - before the list name otherwise it will print it with brackets and apostrophes.
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)
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.
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:
To print an element with a certain index, put the index in square brackets. But remember that the index starts at 0 not 1.
Practice Task 3
Create a list of at least five breakfast cereals.
Print each cereal on a separate line.
Then print just the first cereal in a message underneath, using its index.
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.
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
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.
Look back in 'Printing Lists' above at how to print an element using its index - remember to use square brackets, like cities.
Extra Challenge: Also print the total number of words entered.
The .sort() command will sort elements in a list into alphabetical order (if a string) or numerical order (if a number).
The .sort() command can be used to sort values in descending order by including reverse = True in the brackets.
Practice Task 5
Create a list of at least six names.
Sort the list.
Print each element on a separate line.
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.
Embedded Python Editor
Powered by trinket.io