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.
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!
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!
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!
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.
Okay, 5 key things you need to take from what you just learnt are:
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!