How to Insert or Remove Items From a List in Python



Python


In this article, we show how to insert or remove items from a list in Python.

So lists are mutable elements in Python, which allows us to insert or remove elements. With lists, we can also change existing values of elements.

How to Insert Elements Into a List

So let's go over now how to insert elements into a list.

If you simply want to add elements to the ending of a list, you can so using the append() function in Python.

We show this in the following code below.



So you can see the append() function simply adds an element to the end of a list in Python.

If you want to insert an element into a specific location in the list, then we use the insert() function.

The insert() function takes in 2 parameters.

The first parameter is where in the list you want to insert the element.

The second parameter is the actual element you want to add.

This is shown in the code below.



So you can see that we have a list, list1, which originally has 3 elements.

We then use the insert() function to insert 'cantaloupe' into the first item location of the list (index 0).

When we now show the list, we see that 'cantaloupe' is first in the list followed by all the other original elements.

We then use the insert() function again, this time to insert 'honeydew' into the index location 1, which means it will be the second element of the list.

After we output the list1 list, we see that 'honeydew' appears as the second element in the list.

If you want to add an element to the end of the list, you simply use the append() function. We do this to add 'mango'. You use the insert() function only when you need to insert the added element to a specific location in the list.

How to Remove Elements In a List

There are several ways of removing elements from a list in Python.

To remove an individual element in a list, we can use the pop() function in Python, specifying the index location within the function.

This is shown in the code below.



So we have a list, list1, which has 3 elements.

We remove the element from the index location 1.

After showing the list, we see that the element at index location 1, 'banana', has now been removed.

If you want to remove an element based on the value of the element, instead of the index location, we use the remove() function. Within the remove() function, you place in the value you want to remove.

This is shown in the code below.



So now we've removed the element 'persimmon' with the remove() function.

Again, when you want to remove an element with the index location, use the pop() function. To remove an element based off the value, use the remove() function.

Note that the remove() function only removes the first occurrence of that value in the list. If you have a list where the value appears in multiple locations, the remove() function only removes the first occurrence of that value.

To if you want to remove multiple elements in the list all at once, then we use the del function.

Within this del function, you provide the first element you want to delete and the last element you want to delete, based on the index location.

This is shown in the code below.



So we have a list, list1, composed of 4 elements.

We want to delete the first 2 elements, at index locations 0 and 1. So we use the statement, del list1[0:2], to do so.

An important thing to know about the delete function with lists is that it won't delete elements 0-2. It deletes all elements up to but not including 2. So in this case, it deletes elements at index locations 0 and 1.

Now we have the same list, list1, with the original 4 elements.

This time, however, we want to delete the last 2 elements.

We do this with the statement, del list1[2:4]

Even though there is no element at index location 4, remember this del function deletes elements up to but not including 4. Therefore, the elements at index locations 2 and 3 get deleted.

And this is how we can insert or remove items from a list in Python.


Related Resources

How to Get the Number of Items in a List in Python

How to Get the Last Item in a List in Python



HTML Comment Box is loading comments...