0:00

[MUSIC]

Â So you've seen many of the things that you can do with lists.

Â In this lecture, however, we're going to look at some examples so

Â you can see concretely different ways in which you can use lists.

Â And most importantly, I'm going to show you iteration.

Â Iteration is the operation on lists that allows you to step

Â through the list element by element, and

Â actually do something to each element if you'd like, all right.

Â This is where lists really get their powers.

Â So hopefully, by seeing this in action,

Â you're going to have a better understanding of how this is operating,

Â how you can use list in your programs.

Â So let's get started.

Â Okay, so here I am with a simple skeleton of a program here.

Â We're going to get, take a list of numbers,

Â and we're going to look at odd numbers in that list.

Â Okay, so first thing.

Â I've got my list of numbers and I'm going to print them out.

Â Okay, we already know how that works.

Â Great, I've got the list over here in the console.

Â So now, let's try to count the number of odd numbers, okay.

Â All right, so I take a list of numbers as input, and

Â let's start a variable count at zero.

Â Now, I want to iterate over the list, so

Â I use for num in numbers, okay.

Â This allows me to go through all of the elements in the list number, okay.

Â And each element gets assigned to num one by one, okay?

Â And so I can say, if num modular 2 == 1,

Â okay that means it's odd, then let's increment the count.

Â And at the end, let's return the count, okay?

Â So, how do I check if it's odd?

Â Well, I divide it by two basically.

Â If I do num mod 2, I'm going to get zero or one, right?

Â If I divide it by two and there's no remainder, I get zero.

Â If I divide it by two and there is one as a remainder, I get one, right.

Â And if there's a remainder, then it's odd.

Â So if that happens, I increase the count by one, all right.

Â And each time through this for

Â loop, num is going to get the next element in the list.

Â So I'm going to actually check each element in the list if it's odd and

Â I'll increment the counter if it is, all right?

Â So let's check in my list of numbers here.

Â Let's print count_odd, and numbers, okay.

Â See how many numbers are odd.

Â There are seven odd numbers in my list, okay.

Â Now, I can do a bunch of other things.

Â Let's actually try and check if there are any odd numbers at all in the list, okay.

Â So we start with a Boolean flag here, okay.

Â And this is a common programming technique,

Â where I have a Boolean variable that tells me if something has happened or not.

Â So initially, I have not found any odd numbers so I set odd equal to false.

Â Now, I iterate over the number's list.

Â 2:37

If I find an odd number, I set that flag to true.

Â Okay, and now I can return odd, all right.

Â So let's check that.

Â See if there are any odd numbers.

Â Print check_odd numbers, okay.

Â Both of these are sort of useful techniques that you will see again and

Â again in programming.

Â Where I operate over a list and I increment a counter of some sort, or

Â I iterate over a list and I check to find something in that list, okay.

Â All right, now it's fun to count the odd numbers on the list.

Â It's fun to check if there are any odd numbers in the list.

Â Although I, hopefully you realized that if I can count the odd numbers in the list,

Â I don't really need another function to check if there are any odd numbers in

Â the list.

Â In fact, if the count is greater than zero,

Â I know that there are odd numbers in the list.

Â Now, before I move on to this, doing something more interesting,

Â I want to point something out.

Â As soon as I find an odd number,

Â do I have to actually keep looking in this check_odd function, okay.

Â So if I'm going to do it this way, it's not really that much different or

Â better than counting the number of odd numbers and

Â seeing if there is more than zero, okay.

Â But I can do better.

Â What if this list were really, really,

Â really long and the second number in the list were odd.

Â Well, then I could stop, all right.

Â So instead of using a flag here, what I could do, so ignore that, all right.

Â Go through here and if I find an odd number, I return true.

Â 4:05

And if this loop finishes going through all of the elements in the list and

Â doesn't return true, well I know there's not an odd number in the list.

Â I can return false, all right.

Â And you can see that it still works.

Â I still find it's true, all right?

Â Now, I want you to think about this.

Â This return statement here is in the middle of the loop.

Â What that means is, I don't finish.

Â As soon as I get to a return statement in a function, the function ends and

Â it returns a value, okay.

Â So before, I had that flag, I just set it to true.

Â And I might set it to true a whole bunch of times, that doesn't matter.

Â And then at the end, after I've gone through the entire list,

Â I return true or false.

Â Here, I go through and as soon as I find an odd number I return true.

Â If there are no odd numbers in the list,

Â I'll go through them all, find out there is none, and return false.

Â Notice that I have to go to through the whole list if they're all even, right?

Â The only way that I'm going to know they're all even is if I check them all,

Â okay.

Â All right, now what about if I want to do something even more interesting.

Â I want to remove all the odd numbers in the list, okay.

Â So, the obvious thing to do here is to go for num in numbers, okay.

Â If num mod two is one, then I want to

Â do numbers that remove num, okay.

Â And this is going to get rid of all the numbers in the list, all right?

Â So let's, there are all the odd numbers in the list.

Â So let's do that.

Â Let's call remove_odd, odd numbers and

Â let's print numbers again, okay.

Â And let's see what happens here.

Â 5:39

All right, works great, right?

Â Mm-hm, wait a minute, I see some 7s in there, okay.

Â It did not get rid of all the odd numbers.

Â Now what is wrong with my program?

Â It looks fine to me, right.

Â I went through each number in turn, for all num in numbers,

Â so num is going to get each value in numbers.

Â I checked if num mod two is one and if it is,

Â I call the numbers removed on them, all right.

Â 6:05

Looks good, okay.

Â But this does not work, and this is a very, very common error.

Â You cannot change the list that you are iterating over.

Â So while I'm iterating on numbers, I can't change it and

Â expect everything to work okay.

Â So what's happening is, things are getting removed.

Â As I'm trying to go to the next element, I might skip elements,

Â I might do elements twice, right, depending on how you change it, okay.

Â So Python does not allow this.

Â You have some unpredictable behavior if you ever actually do it.

Â So, let's think about how I might actually solve this problem.

Â 6:42

Okay, so how can I deal with this problem?

Â Well, a common way of handling the fact that I can't modify a list while I'm

Â iterating over it, is to create another list, okay.

Â So, let's try remove_odd 2 of numbers, okay.

Â And now I'm going to create a new list called remove, and

Â this is going to hold the indices of the elements that I want to remove.

Â So for num in numbers, if num mod 2 equals 1,

Â and remove.append numbers.index of num, okay.

Â And when I'm done with that I want to go for

Â index in remove, numbers.pop, index.

Â Okay, this looks great.

Â Let's make sure we're calling remove two down here, yes we are.

Â All right, and let's run this.

Â 7:34

Okay.

Â Well that didn't work.

Â What is the problem here?

Â Well, as I'm iterating over remove, I'm popping out indexes out of number.

Â But each time I pop something,

Â it shifts all the other elements to put to the left to fill the gap, okay.

Â So the indices are no longer the correct indices, okay.

Â So I'm not modifying remove here,

Â I'm modifying number but it's not working very well.

Â So, let's try instead to change this.

Â Let's call it remove_odd3.

Â We could copy here so I can remember what that looked like

Â 9:00

I think actually in this case,

Â I might have done better if I just created a new list, okay.

Â This is kind of complicated here.

Â Sometimes this works well, depending on how you want to handle the situation.

Â But I think, here in this particular case,

Â if instead I say newnums is an empty list for

Â num in numbers, okay.

Â If num % 2 is even,

Â then newnums.append num and

Â let's return newnums, okay.

Â Now, Print,

Â actually let's do it this way.

Â Delete this stuff.

Â Print remove_odd for numbers.

Â 9:57

Okay, and if I reset and run, you can see that I've also,

Â now I have a new list that doesn't have any odd numbers in it.

Â So it's actually done what I wanted it to do, okay.

Â Now I want you to think about these things, and

Â I actually want to show you one last thing, all right.

Â And let's say remove_last_odd, okay.

Â I want to get rid of the last odd number that I find, okay.

Â So let's say, first we're going to have a flag has_odd,

Â right, because I might not have an odd number in it, okay.

Â Okay, and last_odd, let's just set that to zero for

Â now because I haven't found one yet, right.

Â So for num in numbers, okay,

Â if num % 2 equal 1, I found an odd number.

Â Let's say, has_odd = True.

Â I set the flag so I know that I've now found it, last_odd = num, okay.

Â Now, when I get out of this loop, if has_odd is false,

Â then there's nothing to remove, okay.

Â So I have, if has_odd, so if there's actually an odd number, I can say,

Â numbers.remove(last_odd), okay.

Â And this is great, right?

Â So I go here, I print numbers.

Â 11:24

And I call remove_last_odd on numbers,

Â print numbers again, and let's run this, okay.

Â And it removed the last odd number, right?

Â Mm-hm, no, that 7 is still there at the end.

Â Now, given what we talked about in this lecture,

Â I want you to look at that code, I want you to think about that code,

Â and I want you to see if you can actually fix it, okay?

Â All right, now we've seen a whole bunch of different ways that I can iterate over

Â these list and I can modify the list, all right.

Â But I just can't modify them while I'm iterating them.

Â Okay, you saw several examples of how to iterate over

Â a list in this lecture, right.

Â You saw how to check if a list has something in it.

Â You saw how to count things that are in the list.

Â You saw how to create new lists or modify lists, based on some criterion, okay.

Â These are all powerful ways in which you can use lists, all right.

Â And the combination of understanding the different methods that lists have and

Â being able to iterate over the list, gives you a powerful data type

Â that allows you to do a lot of different things in your programs.

Â So hopefully, at this point, you feel a little bit comfortable with lists, and

Â you'll be able to use them to maximum effect.

Â