Javascript required
Skip to content Skip to sidebar Skip to footer

How to Remove an Item From a List Python

All the Ways to Add and Remove Items from a List in Python

Görkem Arslan

How many ways do you know? There are more than the append() and remove() methods. Methods aren't the only way, so let's learn all of them.

Lists are one of the widely-used built-in data types in Python. It is no surprise that you see a list in any Python project. What makes lists so important is being mutable. That is, lists support to be resized. You can add and also remove items.

Let's review them without losing time.

Built-in Methods to Add Items

We first start with built-in methods to add an item. In Python, there are three built-in methods to add items to a list. They are append(), extend(), and insert(). We'll cover all of them but first, take a look at the append() method.

append()

If you want to add a single element to the end of a list, you should use the append() method. It can be used to add any type of data to an existing list.

Example:

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits.append("banana")
>>> print(fruits)

Output:

            ['apple', 'mango', 'cherry', 'banana']          

But when you try to append items from another list, you get:

Example 2:

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits.append(["banana", "peach"])
>>> print(fruits)

Output:

            ['apple', 'mango', 'cherry', ['banana', 'peach']]          

Did you notice? Brackets, i.e. a list, appear unexpectedly at the end. You didn't add items to the list. What you just did is append another list at the end of the existing list.

extend()

There is a method just for this purpose: extend(). It adds elements from a different list to the end of your list.

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits.extend(["banana", "peach"])
>>> print(fruits)

Output:

            ['apple', 'mango', 'cherry', 'banana', 'peach']          

In fact, extend() method is used to add elements present in any iterable object.

Let's pass a tuple as an argument to extend().

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits.extend(("banana", "peach"))
>>> print(fruits)

Output:

            ['apple', 'mango', 'cherry', 'banana', 'peach']          

Let's pass a string as an argument to extend().

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits.extend("banana")
>>> print(fruits)

Output:

            ['apple', 'mango', 'cherry', 'b', 'a', 'n', 'a', 'n', 'a']          

Guess what happened? In Python, a string is an iterable object, and characters in a string are iterated over.

In conclusion, what extend() does is to add members of an iterable object.

insert()

If you want to add an item to wherever you want, you should use the insert() method. This method takes two arguments: the index where the new element needs to be added and the element value.

Example:

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits.insert(0, "banana")
>>> print(fruits)

Output:

            ['banana', 'apple', 'mango', 'cherry']          

Example 2:

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits.insert(2, "banana")
>>> print(fruits)

Output:

            ['apple', 'mango', 'banana', 'cherry']          

Built-in Methods to Delete Items

Now, we'll cover methods to remove items from the existing list. In Python, there are three built-in methods to remove items from a list. They are remove(), pop(), and clear(). We'll first look at the remove() method.

remove()

You can remove an item from a list with the remove() method. While using the remove() method, the element value that needs to be removed from the list is given.

Example:

            >>> fruits = ["banana", "apple", "banana", "mango", "cherry"]
>>> fruits.remove("apple")
>>> print(fruits)

Output:

            ['banana', 'banana', 'mango', 'cherry']          

What if there are multiple elements with the same value in the list? At this time, you'll remove the first occurrence of the value.

Example 2:

            >>> fruits = ["banana", "apple", "banana", "mango", "cherry"]
>>> fruits.remove("banana")
>>> print(fruits)

Output:

            ['apple', 'banana', 'mango', 'cherry']          

pop()

You can remove the last element from the element's list index of the element as a parameter to the pop function.

Example:

            >>> fruits = ["apple", "mango", "cherry", "banana"]
>>> fruits.pop(2)
>>> print(fruits)

Output:

            ['apple', 'mango', 'banana']          

pop() method returns the value that has been removed. Since you pass the index, you may want to know the value of the removed item.

Example:

            >>> fruits = ["apple", "mango", "cherry", "banana"]
>>> deleted = fruits.pop(2)
>>> print(fruits)
>>> print(deleted)

Output:

            ['apple', 'mango', 'banana']
'cherry'

If no value is past to the pop() method, then the last item of the list is removed.

Example:

            >>> fruits = ["apple", "mango", "cherry", "banana"]
>>> deleted = fruits.pop()
>>> print(fruits)
>>> print(deleted)

Output:

            ['apple', 'mango', 'cherry']
'banana'

clear()

Built-in list method clear() does not take any argument and removes all items.

Example:

            >>> fruits = ["apple", "mango", "cherry", "banana"]
>>> fruits.clear()
>>> print(fruits)

Output:

            []          

As you can see, the list is completely empty.

Operators

In addition to built-in methods, it is possible to resize a list with + and * operators.

'+' Operator

You can concatenate lists by the +operator. It works like extend() method. But in the end, it returns a new list.

Example 1:

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits = fruits + ["banana"]
>>> print(fruits)

Output:

            ['apple', 'mango', 'cherry', 'banana']          

Let's check the id's of objects.

Example 2:

            >>> fruits = ["apple", "mango", "cherry"]
>>> print(id(fruits))
>>> fruits = fruits + ["banana"]
>>> print(id(fruits))

Output:

            4557568992
4555334032

As you can, they are not the same so a new list is created.

'*' Operator

It is possible to repeat a list with the * operator.

Example:

            >>> fruits = ["apple", "mango"]
>>> fruits = fruits * 2
>>> print(fruits)

Output:

            ['apple', 'mango', 'apple', 'mango']          

Like + operator, using * operator in lists creates a new list so it should not be counted as a way to add an item because a new item is not added into the existing list. In fact, the new item appears in the newly created list.

Let's prove that by comparing objects id's:

            >>> fruits = ["apple", "mango"]
>>> print(id(fruits))
>>> fruits = fruits * 2
>>> print(id(fruits))

Output:

            4555234112
4557568992

In conclusion, usage of + and * operators generate a new list.

del

In Python, the del keyword is principally used to delete objects so that the object is no longer available in memory.

Since everything is essentially an object in Python, we can delete elements of a list with the del keyword. For this purpose, we need to specify the item to be deleted by index.

Example 1:

            >>> fruits = ["apple", "mango", "cherry", "banana"]
>>> del fruits[1]
>>> print(fruits)

Output:

            ['apple', 'cherry', 'banana']          

Example 2:

            >>> fruits = ["apple", "cherry", "banana"]
>>> del fruits[1]
>>> print(fruits)

Output:

            ['apple', 'cherry']          

I don't know whether you've noticed that the built-in methods to delete items either remove one element at a time or clear out all elements in a list. But we can delete multiple elements with the del keyword.

Example 2:

            >>> fruits = ["apple", "mango", "cherry", "banana"]
>>> del fruits[1:3]
>>> print(fruits)

Output:

            ['apple', 'banana']          

Slice Assignment

Supporting slicing is one of the key features of lists. It is possible to access the subpart of a list with slicing. Let's remember how to use slicing in lists:

Example:

            >>> fruits = ["apple", "mango", "cherry", "banana"]
>>> print(fruits[1:3])

Output:

            ['mango', 'cherry']          

We've already used slicing to get data within a specific index range, but we can also change the subpart of a list with slicing.

Let's try to add an item to a list with a slicing assignment.

Example 2:

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits[3:3] = ["banana"]
>>> print(fruits)

Output:

            ['apple', 'mango', 'cherry', 'banana']          

Example 3:

            >>> fruits = ["apple", "mango", "cherry"]
>>> fruits[3:4] = ["banana", "peach"]
>>> print(fruits)

Output:

            ['apple', 'mango', 'cherry', 'banana', 'peach']          

Let's remove a subpart and assign a new item to that place:

Example 4:

            >>> fruits = ["apple", "mango", "cherry", "banana"]
>>> fruits[1:3] = ["lime"]
>>> print(fruits)

Output:

            ['apple', 'lime', 'banana']          

We removed "mango"and "cherry" from the list by using slicing.

Thank you for reading my article. Don't forget to try the examples above and test them out for yourself.

You might be interested in another useful article:

How to Remove an Item From a List Python

Source: https://levelup.gitconnected.com/all-the-ways-to-add-and-remove-items-from-a-list-in-python-c6ee1442244a