Search

5 Interesting Ways To Remove Items From A List.

In python, a list is an ordered sequence of elements that is mutable, or changeable. Each element in a list is called an item. Although the types of items in a list can vary, most times you will find that the items in a list are of the same type. You denote a list by having the items enclosed between []. The items in a list could be mutable or immutable data types like strings, integers, floats, or even lists themselves in a nested list structure.

python list
 

Lists have several functions that could be used to operate on the items like adding items to a list, reversing the items in a list, and sorting the items in a list according to defined keys or order. In this post, we will be discussing five ways you can use to remove an item or items from a list.

The different ways are divided into two groups: removing a single item from the list and when you want to remove more than one item in a single operation.

Removing a single item.

In this section I will mention two ways you can use just to remove a single item. They are designed for just that.

list.remove(x):

The remove function is a function that comes with the list class in python. It is used to remove a single item, x, from the list and it does not return anything. It removes the first item, x, from the list even if there are more than one occurrence of the item in the list.

Now for some code to demonstrate it:

You can see that I asked the remove function to remove ‘mango’ from the list of fruits and it removed the first occurrence of the fruit, mango, leaving behind the second. If there is no item like mango in the list, remove function would have returned an error.

list.pop([i]):

This function gives you the ability to remove an item by index. Although you can remove only one item at a time, using the pop built-in function, you do not remove by value but by index in the list. And remember, the indices of lists in python starts from 0. In the syntax above, the item at index i is being removed from the list. If you do not specify an index to pop, it then removes the last item in the list thereby acting as a stack, i.e last in first out behavior. The function also returns the item you popped, giving you the added ability to reuse that item that was popped from the list.

Here is some code on the fruits list to demonstrate that.

You could experiment with removing items from a list you created yourself using any of the two functions above.

Now we will go on to methods that gives you the ability to remove more than one item in a single operation.

Ways to remove more than one item from a list.

These methods can remove a single item or more than one item in a single operation. I have catalogued three ways that this can be done, using both functions and programming logic.

del object_name[index]:

The del operator can be used to remove any object in python. It can act on lists and other types in python. Unlike the earlier two functions, the del operator can be used to remove a single item from a list or a slice of items provided they exist consecutively. It does not return anything. When using del on a list, you must specify the list index of the item you want to remove or the slice you want to remove.

Here is some code illustrating the operation of the del operator.

Notice that when I completely deleted the fruits list and tried to reference it again, it gave a NameError. Fruits list does not exist any longer after applying del operator to the object. So, you can see that the del operator is a powerful tool. Use it at your discretion.

Using slicing logic to delete items:

We can use the concept of the slice operator, just as we used for the del operator above, to remove specific items from a list. I find this cumbersome sometimes but I need to add it here. A slice can be denoted as list_name[start:stop:step]. The stop and step are optional but if you don’t specify step, step defaults to 1, and if you don’t specify stop and step, what it gives you is a value, the item, and not a list, but we need to have a list back in this operation, so we will be specifying both the start and stop.

Here is some code.

Now, let’s move on to the last method for removing more than one item from a list. It involves using programming logic and list comprehension.

Programming logic method:

In this method, which I use often, you just move through the list and filter out the item you don’t need. That's all. I use list comprehension to do it, but you can also use for loop if you don’t understand list comprehension.

For those of you who don’t understand list comprehension, here is the same logic using a for loop. But list comprehension is more preferred because it looks more beautiful.

The for loop took four lines while list comprehension took only one line. Grrrh!

That’s all folks. I hope I have given you ideas on how you can remove items from your lists.

Happy pythoning.

No comments:

Post a Comment

Your comments here!

Matched content