Understanding List Methods and Their Applications

Use Python list methods such as append, pop, extend, insert, sort, reverse, remove, index, and copy to manipulate and modify lists effectively.

Dive into Python's powerful list methods and learn how to modify, organize, and manage your lists with precision. Explore practical examples of append, pop, sort, and more, and understand key differences like shallow versus deep copying.

Key Insights

  • Distinguish between functions (standalone tools like print()) and methods (functions called specifically on data types with a dot notation, such as list.append()).
  • Discover essential Python list methods, including append (adds an item), pop (removes last or specific-index item), extend (adds multiple items from another list), and sort (arranges list alphabetically or numerically).
  • Understand referencing in Python lists by differentiating between shallow copies (variables referencing the same list object) and deep copies (fully independent lists created using the .copy() method).

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

This is a lesson preview only. For the full lesson, purchase the course here.

Methods—so, methods are functions that are called on objects. So, technically, when we say print, it's a function because we're not calling it on anything other than what you might call core Python. Same with len, type, and round, and many other ones that we've used—int, str—all these functions that we've been using, right? You just write the name of the function, parentheses, pass a value in.

But some functions are called on specific data-type variables, and what we will now discuss are called list methods. A method is preceded by a dot.

So, it's like variable dot. The dot is preceded by a variable, kind of like this, right? The method is preceded by a dot as opposed to a function that is not; for example, print doesn’t have a dot in front of it.

You just write print. Now, you wouldn’t just put the dot either. In front of the dot is the variable that you’re calling the method on.

If you’re calling the method on a list, you’re using a very specific kind of method—a list method. You want to do something to a list, so you call a method on the list.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

You want to do something to a number or a string, you call a specific kind of method on a number or a string. A lot of Python involves just learning what all the various methods are that you call on the various data types.

A method may or may not return a value. In other words, you may or may not get an answer back, and if you do get an answer back, you’re going to want to save the operation—equal to something—to catch that answer coming back at you.

So list methods include append, pop, sort, among others. Here they are. Here’s a bunch.

That’s not every one, but it is a lot of them—ten of them, in fact. So list dot append item adds an item to the end of a list.

List dot pop removes the last item from a list. It returns it if you want to catch the item. Like let’s say you have a deck of cards and you pop the last card off.

You might want to catch the card, so you set the operation equal to something to receive that removed item. List dot pop index will remove and return the item at the specified index.

So you could come into a list of many things and say list dot pop three and get the item removed and handed to you that was or is at index three. List dot sort will alphabetize a list of strings or put numbers in ascending order. List dot extend another list will add one list to another.

And returns a new combined list, right? So you would catch that equal to something—set that equal to something. List dot reverse reverses the order of items in a list.

If you sort first, alphabetize, and then you reverse, you get reverse Z-to-A order. Or if they’re numbers, you get descending order. List dot remove item removes the item from a list wherever it may be by name.

Not by index. List index item returns the index of an item. List insert index item adds an item to a list at the index.

And list copy returns an independent copy of a list. So that’s a lot of stuff. Let’s just go through a few of these.

We’re going to be hitting them like crazy for the rest of the course. So just because they kind of blow through a demonstration real quick, well, you’ve got the pause button. But the point is we come back to stuff.

Once you’ve seen something, it’s not like you never see it again. I didn’t really get it. You’ll get it.

We’re going to be up to our eyeballs in list methods for the rest of the course. This is just to get to know you. OK, so we’re going to make—we’ve got our pets, right? We’ve already got our pets.

So let’s just print the pets. We’ll redo the pets. We already have it.

But just to make it easier to follow, we’ll just redo the pets. It’s the exact same thing we had before.

Oh, no, it’s not the exact same thing. We got the cat back; we had the canary before.

OK, now we’re going to try these out. OK, so list.append item adds an item to the end of a list. So let’s use append to add a pet to the end of the list.

We’ll add chameleon. We’ll say pets.append chameleon. Print your pets again.

Boom. You can also Command-Enter to run the cell; you don’t have to click the little button.

And you’ve got a new item at the end of the list. And let’s append. Now, be careful.

Don’t run this cell again, right—the chameleon one. If you click it again, it’s going to do another chameleon, right? It’s going to add the item again.

OK, let’s add another item. So that’s why we’re doing it fresh. Like we want to add goldfish.

We don’t do it here because it would not just have the goldfish; it would add another chameleon because it would run this pets.append chameleon line. So let’s add a goldfish.

Print pets. Boom. Goldfish.

Now append. Let’s try this. Pets.append. Append is for adding items to the end of a list.

But can you add multiple items? I mean, I don’t want to sit here all day. What if I want to throw in three items at once? Can I? Do I have to run three separate commands? You wouldn’t think so. But it gives you an error.

List.append takes exactly one argument. So you might think, OK, let me turn that off. Just keep that error as a reference.

Not going to delete it, right? We’ve got a note about the error. OK, we’re going to take it from the top. We’re going to try to add the three items to the pets at the same time.

But we’re going to do a little tricky hack. We’re going to say, OK, it’s only one item allowed. So what if we pass it a list? Isn’t a list a single item, right? We had our nested, right? That’s one item.

We want to add three pets. Why don’t we just make a list out of them and add that one item? It’ll work, but it doesn’t work the way you want it to work. What it does is give you a nested list.

So you have a list inside a list. And it’s not symmetrical. It’s not like the nice nested list that we had where everything was grouped by mammals and reptiles and birds.

This is just this loose list at the end. You know, they’re not even the same kind of animal. So it’s not what we want.

We’re going to remove the unwanted nested list, which brings us to our next method. Conveniently enough, pop will remove the last item from a list, right? As noted, list.pop removes the last item. And that’s exactly what we want to do.

We want to get rid of this last item. We’re going to say pop, and then we’re going to print pets again.

And then boom. Now you’ve gotten rid of it. Now we’re going to do it from the top again.

We’re going to try to add—so the first time we tried to add the three items, we got an error. The second time we got an unwanted nested list.

Now we’re going to keep it flat by using extend. We’re going to say pets.extend Canary Python tarantula as a list. Now extend takes a list, but it works in a flat way.

We’re going to say extend and then print. And now you’ve got your Canary Python tarantula. Actually, no, it does not return a new list.

Combined. I just added that. Keeping the result flat.

No nested list, right? You’re adding a list to another list without it going in nested-wise. So extend adds a list to another list, keeping the result flat.

The combined result, right? Use extend. And now if we want to sort the list, it did start off alphabetized, but that’s not the case anymore. We could say pets.sort and print that.

And we will have an A to B. A, C, C, C, D, G. Great, it worked. Now to reverse the list, we’d say pets.reverse. And because it’s alphabetized, it’ll now be Z-to-A order. That worked as well.

And let’s remove the hamster by name. So that is this one. List.remove item by name.

Removes the item from the list by name. If you want to get rid of the hamster, you don’t have to know where it is by index or anything. You would just say pets.remove and just say, I want that hamster out of here.

You know, its squeaky wheel is driving me nuts. All right, run it and gun it and no more hamster. Now, if you’d like to get rid of something by index, let’s get rid of the iguana by index.

Now, in this case, we happen to know the iguana is at index two. So let’s say pets.pop two. That will remove the iguana, but you have to know that the iguana is at that location—that’s the trick, right? Kind of like the little caveat there.

But what if you don’t know the location? Or what if you want to reference something right after or before some other items? So I want to get the item right after Python, which is now the goldfish, right? It used to be the iguana. So what you want to do is first find something by index. So this array method—list method—is list.indexItem returns the index of the item.

If we can get the index of the item, we can get the next item by adding plus one to that number, and then we can pop that number, if that makes sense. We’re going to say, let’s call it—we’ll make a variable called Python index, for lack of a better name. That will be pets.indexPython. That will return the index of the Python wherever it is.

And that should be a one, right? If you look right here, Python index. So let’s spell it correctly. There—it is a one.

Now, if we want to get rid of whatever is right after the Python, which should be the goldfish. No. Oh, I keep popping stuff.

Oh, boy. All right. That’s bad, right? Don’t rerun these additive or destructive moves.

If you hit a cell again and again, it’s appending or popping. It’s going to keep appending and popping, and I’ve just whittled away many items. So what you want to do is—there’s no fixing that.

So you just have to go ahead and rerun. So here we go, right? Run, restart session, run all. Why? Because I hit this pop thing a couple of times, and I whittled away at the items.

So pop whatever is right after the Python. That should be the goldfish. OK, that is what we want.

So first, get the index of the Python, and then just pop the item at Python index plus one. We’ll say pets.pop Python index plus one. So Python is at one.

This is at two. And it will print pets, and there should be no more goldfish. Yep, it’s gone.

It’s gone. Don’t run this again. If it says pop, don’t run it again.

If it says append or extend, don’t run it again. Because it’ll keep adding—you’ll get multiple chameleons or whatever you’re appending.

Right, if you extend this move, you’ll get those three items added again and again. All right, let’s insert an item at index now. We want to add a parrot at index five.

So that move is insert. You put the index and the item. We would say insert pets.insert five comma the item, which would be a parrot.

We’ll say pets.insert five. Let’s put the parrot at five. There it is—zero, one, two, three, four, five.

All right, now this is kind of a tricky concept—a deep copy versus a shallow copy. If you have a string or an int or a float—so we’ve got a primitive value like that.

In other words, it’s not a list; it’s not multiple values. You can just assign one to the other, and they will stay independent.

They won’t be connected. If we say X is five, and then we say y equals X, then certainly X and y are both going to be five, sure. But then if you change y, say y equals X squared, and you print X and y, X is still five and y is now 25.

We saw this with our math operators before. If you set a variable in terms of another variable, that new variable—that’s called an expression, first of all—that new variable is its own independent entity.

It’s not connected to or referencing the original at all. However, with a list, if you set one list equal to another list, they’re kind of joined at the hip in this weird way. If you change the copy, the original will also modify.

So that’s called a shallow copy. Let’s look. We’ll say fruits equals apple, banana, cherry, and then we will say fruits two equals fruits, right? So they’re the same.

Now, let’s target fruits two and say we’re going to add a grape—append, right—add a grape at the end of fruits two. And then we’ll print fruits two. No surprise, you’ve got the grape.

But if you also print regular fruits, you also get the grape. That’s kind of wild, right? So even though you appended to fruits two, because fruits two is set equal to fruits to begin with, anything you do to fruits two will affect fruits. So fruits two is this reference pointer back to the original.

It’s not its own independent copy. Now a true independent copy—make our own cell out of this. An independent copy would use list two equals list one dot copy.

That actually kind of deserves text. So shallow copy—we don’t want a shallow copy. We don’t need to make it so big.

OK, there. Shallow copy is just list two equals list one. Deep copy is list two equals list one dot copy.

That makes it—the dot copy will make it—a new fresh independent copy. So let’s say fruits three now equals fruits dot copy.

Now remember, fruits received the grape courtesy of fruits two. If you print fruits, you have the grape. Now we’re going to say fruits three dot append kiwi, which is kind of like what we did before: we appended grape.

But in the case of appending grape, it backed up to fruits because fruits two is simply set directly equal to fruits with no copy method called on the original. But now that we’re saying fruits three equals fruits dot copy, fruits three is a new independent copy—a deep copy. If you print fruits three, it totally worked.

But if you go ahead and print regular fruits again, it did not, right? Regular fruits did not pick up the appended kiwi because fruits three is its own entity. What you do to fruits three does not back up, right? Fruits three doesn’t know, so to speak, where it came from.

Brian McClain

Brian is an experienced instructor, curriculum developer, and professional web developer, who in recent years has served as Director for a coding bootcamp in New York. Brian joined Noble Desktop in 2022 and is a lead instructor for HTML & CSS, JavaScript, and Python for Data Science. He also developed Noble's cutting-edge Python for AI course. Prior to that, he taught Python Data Science and Machine Learning as an Adjunct Professor of Computer Science at Westchester County College.

More articles by Brian McClain

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram