Remove an item from a list in Python (clear, pop, remove, del)Posted: 2019-05-29 / Modified: 2021-04-06 / Tags: Python, List Tweet 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. Show
See the following article for adding items to the list.
Sponsored Link Python Lists and List ManipulationMichael 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 ListIn 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 ListTo 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 lengthIf 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 indexIf 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 SummaryIn 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 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]# remove 9 from the list
prime_numbers.remove(9)
# Updated prime_numbers List
print('Updated List: ', prime_numbers)
# Output: Updated List: [2, 3, 5, 7, 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. Exampleanimals = ['cat', 'dog', 'rabbit', 'horse']# get the index of 'dog'
index = animals.index('dog')
print(index)
# Output: 1Python remove() methodPython 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:
Example: Using remove() method to remove an element from the listHere 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 listListsLists 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.... |