1myList.remove(item) # Removes first instance of "item" from myList
2myList.pop(i) # Removes and returns item at myList[i]
1# animals list
2animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
3
4# 'dog' is removed
5animals.remove('dog')
6
7# Updated animals list
8print('Updated animals list: ', animals)
1# removes item with given name in list
2list = [15, 79, 709, "Back to your IDE"]
3list.remove("Back to your IDE")
4
5# removes last item in list
6list.pop()
7
8# pop() also works with an index...
9list.pop(0)
10
11# ...and returns also the "popped" item
12item = list.pop()
1# Below are examples of 'remove', 'del', and 'pop'
2# methods of removing from a python list
3""" 'remove' removes the first matching value, not a specific index: """
4>>> a = [0, 2, 3, 2]
5>>> a.remove(2)
6>>> a
7[0, 3, 2]
8
9""" 'del' removes the item at a specific index: """
10>>> a = [9, 8, 7, 6]
11>>> del a[1]
12>>> a
13[9, 7, 6]
14
15""" 'pop' removes the item at a specific index and returns it. """
16>>> a = [4, 3, 5]
17>>> a.pop(1)
183
19>>> a
20[4, 5]
21
22""" Their error modes are different too: """
23>>> a = [4, 5, 6]
24>>> a.remove(7)
25Traceback (most recent call last):
26 File "<stdin>", line 1, in <module>
27ValueError: list.remove(x): x not in list
28>>> del a[7]
29Traceback (most recent call last):
30 File "<stdin>", line 1, in <module>
31IndexError: list assignment index out of range
32>>> a.pop(7)
33Traceback (most recent call last):
34 File "<stdin>", line 1, in <module>
35IndexError: pop index out of range
1myList = ["hello", 8, "messy list", 3.14] #Creates a list
2myList.remove(3.14) #Removes first instance of 3.14 from myList
3print(myList) #Prints myList
4myList.remove(myList[1]) #Removes first instance of the 2. item in myList
5print(myList) #Prints myList
6
7
8#Output will be the following (minus the hastags):
9#["hello", 8, "messy list"]
10#["hello", "messy list"]