Lesson video

In progress...

Loading...

Hello, my name's Mrs. Jones, and I'm really pleased that you're here to learn with me today.

Today's lesson is called List methods, and we're going to look at different commands to use with the list to add and remove items, as well as how to use lists with functions.

So let's get started.

Welcome to today's lesson.

Today's lesson is called List methods, from the unit Programming: strings and lists.

And by the end of this lesson, you'll be able to use a list to return multiple values from a function.

There are four keywords to today's lesson.

Append.

Append is add an item to the end of a list.

Insert.

Insert add an item to a given position in a list.

Pop.

Pop is remove an item from a given index in a list.

And count.

Count is counting the number of occurrences of a given item.

There are two sections to today's lesson.

Use list methods in a programme is the first, followed by create a function that returns a list.

So let's start with use list methods in a programme.

As lists are dynamic, you can add new items to them during the execution of your programmes.

Adding a new item to the end of the list is known as appending the item to the list.

And here you can see item 0, 1, 2, 3 is in the list, and append, item 4 is added at the end.

Here is an empty shopping list, and you can see it's empty because inside the square brackets there is nothing stored at the moment.

You can add new items to the end of the shopping list by using the append method.

And on line two here, you can see shopping.

append.

Shopping is the name given to the list, the identifier,.

append.

And inside the brackets, what do we want to add to the list? And here we've got bread.

When you print this list, you can see that first it is empty, and then it holds the item "bread" after the append method is called.

You can see, slightly different to the previous slide, we have shopping = [] print(shopping), so that we can see that it is an empty shopping list at that point.

Then we've got shopping.

append("bread").

And then when we output the shopping list, then we can see that bread has been added.

If you append another item, it will be added to the end of the list.

And notice here on line five, we've got another shopping.

append, and this time we have ("cheese").

And then we're going to print(shopping) again.

And that output is added underneath the other two that we've seen.

We've now got print bread and cheese.

Let's have a quick check.

What instruction would add the item "eggs" to the end of the shopping list? Is it A: shopping.

add("eggs")? B, shopping.

append("eggs")? Or C, shopping.

append["eggs"]? Pause the video to consider your answer, and then we'll check it.

Let's check your answer.

The answer was B, shopping.

append("eggs").

Well done if you got that correct.

If you want to add an item within the list and not at the end, you can use the insert method.

The insert method takes two parameters, the index position, followed by the item to be added.

And you'll notice on line five, we've got shopping.

insert, and inside the brackets, the position, 0 comma then the item to be added.

The comma is important in between those two parameters.

Let's have a quick check.

What instruction would add the item eggs to the beginning of the shopping list? Is it A, shopping.

insert("eggs")? Is it B, shopping.

append(0, "eggs")? Or is it C, shopping.

insert(0, "eggs")? Pause the video to consider your answer, and then we'll check it.

Let's check your answer.

The answer was C, shopping.

insert(0, "eggs").

Well done if you got that correct.

You can also remove items from a list using the pop method.

It takes an integer parameter indicating the index of the item to remove.

You can see that at first the list contains bread at the start.

Then the item bread is removed from the list by calling pop(0).

And at the top you can see there we have the shopping list, print(shopping), so you can see the original items in the list, shopping.

pop(0).

And then print out the shopping list.

And on the text output, you can see that the item that was stored in position 0, bread, has been removed.

Let's have a quick check.

After bread is removed from the list, which index location holds the value cheese? Is it A, 0? B, 1? Or C, 2? Pause the video to consider your answer, and then we'll check it.

Let's check your answer.

The answer was 0.

Well done if you got that correct.

To count the number of times a certain item appears in a list, you can use the count method.

This takes one parameter, the item to count, and returns the number of items that appears in the list.

So we have the shopping list again, and now we have print, shopping.

count, and inside the brackets, what do we want to count? So we've got bread.

And on the next one, we have cheese.

And on the last one, eggs.

So it's gonna count how many times bread is in the list, how many times cheese is in the list, and how many times eggs in the list.

And the output is 1, 2, 0.

Let's have a quick check.

What would the following programme print out? We have the shopping list with the items, and we have print(shopping.

count("Bread")).

Is it A, 0? B, 1? Or C, 2? Pause the video to consider your answer, and then we'll check it.

Let's check your answer.

The answer is 0, because notice how the count method is looking for "Bread" with a capital B, which does not appear in the list.

Well done if you got that correct.

Let's have a look at an activity.

You're gonna have a look at creating and managing a to-do list using a Python list and list methods.

So have a go at creating an empty to-do list.

Ask the user to enter three tasks, and append these to this list, and then print the to-do list.

Pause the video and have a go at creating that Python programme, and then we'll go through a solution.

Let's go through a solution.

We have todo_list = an empty list at the beginning with those just the square brackets.

print("Enter your first task: ") task1 = input() todo_list.

append(task1).

That is repeated again for task2, task3, and then print(f"Your to-do list: {todo_list}").

We can see the output on the right-hand side.

Well done if you got that correct.

Gonna continue with that programme, and this time we're gonna modify the programme to ask the user for a new task after appending the first three tasks.

Ask the user for the position, the index, where you want to insert it.

And print out the to-do list.

Pause the video and have a go at modifying your programme to now complete this, and then we'll go through a solution.

Let's have a look at the solution.

So this time, we've got, and notice the numbers.

So the previous programme would still be there on line 0 to 11.

On line 12, we have print("Enter a new task to insert: ").

new_task = input() print parens "Enter the position (index) where you want to insert it: " parens position = int(input()) todo_list.

insert(position, new_task) And then, print(f"Updated to-do list: {todo_list}") And this is an example of what it would look like of that last part.

Well done if you've got that correct.

Gonna continue now with that programme, and again modify it, and modify that previous programme to ask the user for an index of a task they want to remove, and remove it using pop, and then print the to-do list.

Pause the video, use your programme, and now remove one of those items using pop, and then we'll go through a solution.

Let's have a look at the solution.

Again, notice the numbers of the lines.

We're continuing on from the previous programme, so this is now starting on line 18 on this one.

print("Enter the index of the task you want to remove: ") remove_index = int(input()) remove_task = todo_list.

pop(remove_index) print parens f"Updated to-do list: And then on the right we can see the user, the text output.

Well done if you got that correct.

Now we're gonna continue and ask the user for a task name and check how many times it appears in the list using the count method.

So you're continuing using the programme you've been creating, and you need to pause the video and have a go at asking the user for a task name and check how many times it appears in the list using the count method, and then we'll go through a solution.

Let's have a look at the solution.

Again, carrying on from the previous programme, the line numbers increased.

print("Enter a task to count how many times it appears: ") task_to_count = input() task_count = todo_list.

count(task_to_count) print parens f"The task" {task_to_count} appears {task_count} times in your to-do list." parens And you can see an example of the output on the screen too.

Well done if you've got that correct.

Let's move on to the second part of today's lesson: Create a function that returns a list.

Alex says, "Remind me, what is a function?" And Sofia says, "A function is a subroutine or a subprogram that returns a value to the calling programme." Sometimes you need a function to return more than one value.

Returning a list is one way to do this.

You can see there in that little image of main programme, which calls the function, and the function will return a value to the main programme.

To return a value from a function, you use the return keyword.

The return keyword also stops the rest of the function executing, as it returns back to the calling programme.

Here we've got an example, and you can see that inside the function you have a selection statement: if len(my_password) > 7 and "*" in my_password: Then, return True And at that point, if that is true and that returns true, it will return to the main programme, the calling programme.

If not, it'll go return False, and then return to the calling programme.

A Boolean value is returned if it is true or false being returned.

Here is another example of a function that is returning an integer value, and this time we're converting between Fahrenheit and Celsius.

You can see here that we've got a calculation happening on line 3, but on line 4 it's returning an integer value of what's being stored in Celsius, and that is then used in the programme further on.

An integer value is returned, and you can see the output on the right: Enter temperature in Fahrenheit, they enter 100, and then that is returned in the format of 100 degrees Fahrenheit is equal to 37 degrees Celsius.

Let's have a quick check.

What data type is being returned from this function? So we have: def checkNumber(n): if n > 0: return "Positive" elif n < 0: return "Negative" return "Zero" Is that A, returning a Boolean, B, returning a string, or C, returning an integer? Pause the video to consider your answer, and then we'll check.

Let's check your answer.

The answer is string.

Well done if you got that correct.

This example shows the concept of a function returning a top score value, which is an integer.

We have: winner = top_score() It will call the function which is called top_score, and then it will return the high score to the main programme.

This example shows the concept of a function returning the top three teams, which is a list.

It calls the function top_three_teams and returns a list_of_teams. Alex says, "So whatever is put after the return keyword is returned to the calling programme?" Sofia says, "Yes, so we can put a list there and that will get returned too!" So you can see there that it's pointing at the bottom on line 10: return even_numbers.

And you can see the use of commenting here to help describe what's happening.

We are creating an empty list.

We loop through the numbers from 0 to n in steps of 2.

We append the number to a list, and we return the list.

And the output for this will be the even numbers: 0, 2, 4, 6, 8.

Let's have a look at an activity.

And I'd like you to write a Python function that returns a list of random lottery numbers.

Create a function called getLotteryNumbers().

Create a blank list then iterate 6 times appending a random number between 1 and 59 inclusive to this list.

You can see there the code: random.

randint(1, 59) Return the list at the end of the function.

Demonstrate it works by printing out the results of the function.

Pause the video, and then we'll have a look at a possible solution.

Let's have a look at a solution.

We have: import random Then we have our function set up: def getLotteryNumbers(): We have the list set up as empty: numbers = [] for i in range(6): new_number = random.

randint(1, 59) numbers.

append(new_number) numbers.

sort() # put numbers in order return numbers yournumbers = getLotteryNumbers(), which is gonna call that function, and then: print(yournumbers) And you'll get a text output looking something like this, but as it's random, they will not be the same each time you run the programme.

Well done if you completed that programme.

Let's have a look at another activity.

The previous programme occasionally caused duplicate numbers to be added.

So this time, modify your programme, from previous section, to prevent duplicate numbers.

Use a while loop, instead of a for loop, to keep checking if a number is already in the list, and that the length of the list is not 6, before appending the next number.

Pause the video and have a go at modifying your programme, and then we'll go through a solution.

Let's have a look at a solution.

So this time, inside the function, we have the numbers list set up as empty, but this time we have: while len(numbers) != 6: new_number = random.

randint(1, 59) if new_number not in numbers: numbers.

append(new_number) numbers.

sort(), to again put the numbers in order, and: return numbers Well done if you amended your programme and have that working correctly.

In summary, lists provide a variety of methods for manipulating and querying items within the list.

The append method allows you to add items to the end.

The insert method allows you to add items to a position within the list.

The pop method allows you to remove items from a list by providing the index of the item to be removed.

The count method returns the number of items in the list that match the given item.

Returning a list from a function is a technique for returning more than one value from a function.

Well done for completing this lesson on list methods.