python remove value from list code example
Example 1: remove object from array python
my_list = [1,2,4,6,7]
del my_list[1]
print my_list
my_list.remove(4)
print my_list
my_list.pop(2)
Example 2: python remove element from list
myList.remove(item)
myList.pop(i)
Example 3: remove item from list python
list = [15, 79, 709, "Back to your IDE"]
list.remove("Back to your IDE")
list.pop()
list.pop(0)
item = list.pop()
Example 4: remove element from list
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
Example 5: delete a value in list python
list.remove(element)
Example 6: python remove value from list
""" 'remove' removes the first matching value, not a specific index: """
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
""" 'del' removes the item at a specific index: """
>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
""" 'pop' removes the item at a specific index and returns it. """
>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
""" Their error modes are different too: """
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range