How to Remove an Item from a Python List

Binge On Code
Python in Plain English
4 min readSep 22, 2021

--

There are a couple of ways to remove item from a Python list. This simple guide will focus mainly on the built-in Python methods for doing this!

There comes a time where you wonder about the best method to remove an item from a Python list. We have outlined everything you need to know as well as IMPORTANT information to take note of when doing so.

This guide has two parts. The first part will focus on built-in methods to remove an item from a Python list. After that, we will have the second part, which will be just additional information to keep in mind when working with your Python lists.

How we will do it

To better understand the concept we want to put forward in this guide, we will use a simple list:

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

throughout this guide.

This way, you will have a clear understanding of what each method does, and the impact it has.

Okay, let’s do this!

list.pop(optionalIndex)

This method will remove an item from the Python list based on the provided index. The indexing, as usual when it comes to Python list manipulation starts at zero by default. So, the optionalIndex you provide should start at zero.

If there is no optionaIndex provided, then it will remove an item from the Python list, starting from the end, meaning the last item in the list will be removed.

However, if you try to remove an item from an optionaIndex which does not exist, then a Python IndexError will be thrown.

Another very important thing to know is that with this Python list manipulation option, the list will be modified in place. So, for example:

sample_list.pop()

When you do the above and try to print it out:

print(sample_list)

You will see that the last item, 10 will be removed, even though you never assigned the return value of .pop() method.

This is because .pop() method returns none and modifies the list in place when you use it in a typical Python remove list item operation.

So, you should now get:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Okay, BUT what if you want to use pop() on an empty list? Well, you guessed it right I hope. The python IndexError will be thrown, telling you that you cannot pop from an empty list!

That basically covers this approach, onto the next!

list.remove(requiredValue)

This is another approach to remove an item from a Python list. It even has a descriptive name. REMOVE!

However, unlike list.pop(), with list.remove(), you must provide a value which will need to be removed from the list. Without the value, then a Python TypeError exception will be thrown.

So, how to use it is simple. Let’s continue on with our above list and add this line:

sample_list.remove(4)

What this does is simply scan through sample_list and then it will remove the first occurrence of the number 4.

Well, what if 4 does not exist in the list? Well, in that case, a Python ValueError exception will be thrown.

So, when you print out sample_list, you should now have:

[1, 2, 3, 5, 6, 7, 8, 9]

Now, that this is clear, now onto the next method, something which is mostly unpopular, but effective when needed!

list.clear()

Well, as mentioned, this is the unpopular method, as it is a completely destructive approach when you want to remove an item from a Python list.

As you can see, it does not accept a parameter, and that means only one thing…

Well, long story short, it will not just do a simple remove item from python list operation, it will remove every item from the list.

So, with great power comes great responsibility! Use this wisely!

So, for the purpose of completing this part of the tutorial, let’s go ahead and use it:

sample_list.clear()

And just like that, poof! The list is now empty.

Try printing sample_list out, you will see this:

[]

Okay, that was awesome! Now let’s finalize with some key points from this simple guide.

Key Points

Okay, 5 key things you need to take from what you just learnt are:

  • These built-in methods for python list manipulation modify the list in place.
  • These built-in methods return NONE.
  • list.pop() accepts an optional parameter.
  • list.remove() must accept a parameter.
  • Use list.clear() ONLY when you need to remove all the list items.

Conclusion

As you can see, now you are somewhat of a master with Python list manipulation.

As usual, it was a pleasure presenting this guide to you!

Happy coding!

Are you interested in learning Python? Checkout similar awesome articles here: Related Articles

More content at plainenglish.io

--

--

You love programming? Well, we do! We are a team of one as of now and are really passionate about programming and making the world better today than yesterday!