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 :
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 :
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.
It will automatically add 44 to the index 1. The output of the program will be :
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.
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 an 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.
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 :
The above program illustrated both processes. 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 :
In this tutorial, we have learned 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.