Remove an item from a list in Python (clear, pop, remove, del)
In Python, use list methods clear(), pop(), and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.
- Remove all items: clear()
- Remove an item by index and get its value: pop()
- Remove an item by value: remove()
- Remove items by index or slice: del
- Remove items that meet the condition: List comprehensions
See the following article for adding items to the list.
- Add an item to a list in Python (append, extend, insert)
Python Lists and List Manipulation
Michael Galarnyk
May 29, 2017·6 min read
Before starting, I should mention that the code in this blog post and in the video above is available on my github.
Python – Remove item at specific index from List – pop()
To remove an item at specified index or position from a List, you can use pop() method of List class with index provided as argument.
In this tutorial, we shall play with the pop() function and the indexes, in different approaches. Our ultimate goal it to remove an item from the given position.
Syntax – list.pop()
The syntax of pop() method is:
mylist.pop(index)where index is optional.
If you do not provide any index, the last item of the list is removed from the list.
Example 1: Remove Item at Specific Index from List
In the following example, we have a list with numbers. We will use pop() method to delete or remove the item at specified indexm, 3.
Python Program
mylist = [21, 5, 8, 52, 21, 87, 52] index = 3 #delete item in mylist at index mylist.pop(index) print(mylist)RunOutput
[21, 5, 8, 21, 87, 52]The item present at index=3, 52, is removed from the list.
Example 2: Remove Last Item of List
To remove the last item of the list, just don’t pass any index to the pop() method.
Python Program
mylist = [21, 5, 8, 52, 21, 87, 52] #delete last item in mylist mylist.pop() print(mylist)RunOutput
[21, 5, 8, 52, 21, 87]Example 3: pop() with index > List length
If you provide an index greater than the length of the list, you will get IndexError with the message pop index out of range.
Python Program
mylist = [21, 5, 8, 52, 21, 87, 52] index = 100 #index > length of list mylist.pop(index) print(mylist)RunOutput
Traceback (most recent call last): File "example.py", line 5, in <module> mylist.pop(index) IndexError: pop index out of rangeExample 4: pop() with negative index
If you provide a negative index to the pop() method, the indexing is considered from the last item of the list starting from 1.
If you give -1 as index to pop(), the last item of the list is deleted.
If you give -3 as index to pop(), 3rd item from the end of the list is deleted.
If the absolute of negative index crosses the length of the list, you will get IndexError as in previous example.
In the following example, we will provide -2 as index to the pop() method. The second item from last of the list is deleted.
Python Program
mylist = [21, 5, 8, 52, 21, 87, 52] index = -2 #index < 0 mylist.pop(index) print(mylist)RunOutput
[21, 5, 8, 52, 21, 52]Summary
In this tutorial of Python Examples, we learned how to use pop() function, to remove or delete an item from a given position in the list.
Related Tutorials
- Python – Remove all Occurrences of an Item from List
- How to Remove Duplicate Items in Python List?
- How to Remove an Item from Python List?
Python List remove()
In this tutorial, we will learn about the Python List remove() method with the help of examples.
The remove() method removes the first matching element (which is passed as an argument) from the list.
Example
# create a list prime_numbers = [2, 3, 5, 7, 9, 11]Python List index()
In this tutorial, we will learn about the Python List index() method with the help of examples.
The index() method returns the index of the specified element in the list.
Example
animals = ['cat', 'dog', 'rabbit', 'horse']Python remove() method
Python removes () method is a built-in method available with the list. It helps to remove the given very first element matching from the list.
Syntax:
list.remove(element)The element that you want to remove from the list.
ReturnValue
There is no return value for this method.
Tips for using remove() method:
Following are the important points to remember when using remove () method:
- When the list has duplicate elements, the very first element that matches the given element will be removed from the list.
- If the given element is not present in the list, it will throw an error saying the element is not in the list.
- The remove () method does not return any value.
- The remove () takes the value as an argument, so the value has to pass with the correct datatype.
Example: Using remove() method to remove an element from the list
Here is a sample list that i have
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']The list has elements of date-types string and number. The list has duplicate elements like number 12 and string Riya.
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] my_list.remove(12) # it will remove the element 12 at the start. print(my_list) my_list.remove('Riya') # will remove the first Riya from the list print(my_list) my_list.remove(100) #will throw an error print(my_list)Output:
['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback (most recent calllast): File "display.py", line 9, in <module> my_list.remove(100) ValueError: list.remove(x): x not in the listLists
Lists are written within square brackets [ and ], and the items within it are separated by a comma (,). Lists are mutable store collection of heterogeneous items. Let's break the statement down: what does mutable mean? Mutable means that you can change the content of a list without actually changing its identity. What do heterogeneous items mean? It means lists can hold mixed values within itself, such as integers, floats, strings, etc. Lists are built into Python, and you do not need to invoke them separately. Lets first define a list:
list_x = [] #Empty list list_x # Print the list to see its contents [] list_x = [1, 'two', 3.0] list_x [1, 'two', 3.0] list_y = [1, 2, 3] list_y [1, 2, 3]All of the above are examples of lists. list_x is a list of heterogeneous items since it holds integers, strings as well as floats. Whereas list_y holds homogeneous items - integers only.
Now, let's say you needed to access the string value 'two' from list_x for an operation. Here is how you would do it:
value = list_x[1] print('How do you spell 2?', value) How do you spell 2? twoThe list index starts with 0 in Python. So, the index value of 1 is 0, 'two' is 1 and 3 is 2.
But what if you knew the value you wanted to access but didn't know where it would show up in the list? Or if it even existed in the list? This is when the index() method comes in handy....