Python (Fall 2026) Module 3-5: List Methods
Overview
Bill Moseley explains Python list methods and the built-in len() function, emphasizing when methods change a list and how to select, add, remove, and order its items. Examples distinguish count() from total length, demonstrate index calculations and negative indexing, and show that methods such as sort() modify the original list in place.
Key takeaways
- Use count(value) to count occurrences of a particular item, and len(list) to count all items in a list.
- The second-to-last list index is len(list) - 2, but lists shorter than two items do not have a second-to-last item.
- Python's negative index -1 selects the last item, so an out-of-range calculation that becomes -1 may return an item instead of raising IndexError.
- pop() removes an item by index, whereas remove(value) deletes only the first matching value found from the beginning of the list.
- Most list methods modify the existing list; sort() changes its order in place, so use copy() first when the original order must be preserved.
Chapters
- clear() removes every item but leaves the list variable available to refill, while copy() makes a separate list for operations such as sorting a backup.
- count(value) reports how many times a particular value appears, such as fruits.count("cherry"), not the total number of items.
- len(list) returns the total item count; len(string) instead returns the number of characters in the string.
- Python list indexes start at 0, so the second-to-last item is at index len(list) - 2.
- For a five-item pet list, index 3 selects the second-to-last item, shown with the example pet "worm".
- An index beyond the list's valid range raises IndexError; negative index -1 means the last item, while -2 means the second-to-last.
- index(value) finds a value's position; insert(index, value) adds an item at a chosen position and shifts later items to new indexes.
- pop() removes by index, while remove(value) searches from the start and removes only the first matching value.
- reverse() flips the list order; sort() orders numbers from smallest to largest and strings alphabetically from A to Z.
- sort(reverse=True) orders strings from Z to A or numbers from largest to smallest, and changes the original list rather than returning a sorted copy.
Transcript excerpt
0:00 And a lot of those are found in list methods. Now, we've talked about a couple of list methods already. Pop is one. Append is another. So, those are to remove or add. Here is the, the, the technical sheet for append if you forget how it should look. Okay. And by the way, I'm updating the, the function reference in the class to, to be just a little bit more robust and have some more things in it. But append is here. There is a method called clear. List.clear will remove everything from the list. Now, the reason that you might want to do that is that it, it removes everything from the list, but it keeps the variable. So, you can maybe
0:56 start over from scratch or something. Let's say you, all your pets ran away and now you need to start from scratch. You would use clear before you start adding new pets to make sure all of the old ones were gone. So, it's like the train pulls into the station, the car is still there, but it's empty now, right? Kind of like that. Or the, the cars are all removed from the engine. Copy will make an identical copy of a list. That is useful because sometimes you want to keep the original order of things and maybe do something to the, the copy, like sort it alphabetically maybe, or remove certain words or something, but you still need to keep the original. So for the sake of comparison, or like a backup copy. Count can be confusing, because a lot of people take count to
1:57 mean how many items are there in this list, when in fact count will count how many items match a certain value. So in this case I've got a list of fruits and I'm saying fruits.count, count, and I'm
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.