Python add and remove elements from a list

Adding and removing items from a list is required most of the time when we are dealing with a list in our program. For example, suppose you have one list of elements the user has selected from a list. If the user will deselect any list item, you will have to remove that specific value from the list. Again, sometimes you need to insert an item at a specific position, or to the end of the list. So, we have a lot of possibilities that we may encounter in our development journey.
In this tutorial, I will show you different methods of list module in python that can be used to add or remove list items.

Append an element to the end of a list :

For appending an element to the end of a list in python, we have one method called append(element). It will add the element to the end of a list, on which we are calling it. Let’s check it with an example :

randomList = [1,2,3,4,5,6,"eight",True,"nine",(10,11)]

print("Current list ",randomList)

randomList.append(110)
print("Current list ",randomList)

In this example, we have a list of items of different types. The first print method printed out the list. Then we have appended the number 110 to the list and again print it out. The output will look like as below :

Current list  [1, 2, 3, 4, 5, 6, 'eight', True, 'nine', (10, 11)]
Current list  [1, 2, 3, 4, 5, 6, 'eight', True, 'nine', (10, 11), 110]

python add delete list element

As you can see that the element is added to the end of the list.

Insert an element at a given index :

In our previous example, we have added one element to the end of the list. We can also add one element to a specific position in a list. For that, we need to use insert(i,e) method. It will insert an element of value e to the list at index position i.

randomList = [1,2,3,4,5,6]

print("Current list ",randomList)

randomList.insert(1,44)
print("Current list ",randomList)

It will automatically add 44 to the index 1. The output of the program will be :

Current list  [1, 2, 3, 4, 5, 6]
Current list  [1, 44, 2, 3, 4, 5, 6]

python add delete list element

So, the length of the list is automatically increased after inserting the item, all elements are shifted to the right.

Removing an element :

To delete an element from a list, we can use remove(e) method. It will remove the element e from the list.

randomList = [1,2,3,4,5,6]

print("Current list ",randomList)

randomList.remove(3)
print("Current list ",randomList)

It will print the following output :

Current list  [1, 2, 3, 4, 5, 6]
Current list  [1, 2, 4, 5, 6]

python add delete list element

So, you can see that the number 3 is removed from the list. If more than one similar element exists in the list, it will remove the first element found. If the element doesn’t exist, it will throw ValueError.

Remove element of a specific index :

The last example explained how to remove an element by using its value. We can also remove an element by using its index with pop(index) method. It removes the value at that specific index and returns the value.

randomList = [1,2,3,4,5,6]

print("Current list ",randomList)

print("Removed item ",randomList.pop(3))
print("Current list ",randomList)

It will print :

Current list [1, 2, 3, 4, 5, 6]
Removed item 4
Current list [1, 2, 3, 5, 6]

python add delete element from a list

If we don’t provide the value of theĀ index, it will pop out the last element, i.e. it will perform the opposite of append(). It will throw IndexError if the index is invalid.

Add one list to another :

We can either use plus(+) operator to add one list with another, or we can use list1.extend(list2) method. This method will append all items of list2 to the end of list1. Example :

list1 = [1,2,3,4,5,6]
list2 = [7,8,9,10]

list3 = list1 + list2

list1.extend(list2)

print("Final list1 : ",list1)
print("list3 : ",list3)

The above program illustrated the both process. We have added both list1, list2 and set the value to list3. Also, we have used extend to append values of list2 to the end of list1. It will print the below output :

Final list1 :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list3 :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

python add delete element from a list

Conclusion :

In this tutorial, we have learnt how to add and remove an element from a list in python using different ways. All of the above examples are written in python 3. If you are still using python-2, these methods will work for python-2 as well. Try to run the code and if you think any problem or if you have any queries/suggestion, please drop a comment below.
For other tutorials on python, check here.

Leave a Reply