Binge On Code

Jun 8, 2023

142 readers

How to remove an item from a python list

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

There comes a time where you wonder on the best method to remove item from 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 item from python list. After that we will have the second part, which will be just but 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 through 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)

If there is no optionaIndex provided, then the it will remove item from python list, starting from the end, meaning 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!

list.remove(requiredValue)

This is another approach to remove item from 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, lets 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 item from 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 a head and use it:

sample_list.clear()

And just like that, pooof! 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!

Related Articles

How to sort python list – part one

There is nothing as common as sorting when it comes to working with data. This tutorial will give you two part guide on how to sort python list.

Jun 8, 2023

Views 156

How to sort python list – part two

This is the second part of this awesome short series of how to sort python list. We will be sorting a list of python dictionaries! So let’s do this!

Jun 8, 2023

Views 275

Python list - How to insert an item into a python list

Arrays otherwise known as lists in python are common data structures in python. Python list contains items and it can be modified by adding more items to it.

Jun 8, 2023

Views 589

Angular JavaScript React CSS Django Python React Native Next JS State Management Django Rest Framework Unity