You can also use the del
method to remove a item specifying your index.
lista = ['foo', 'bar', 'baz']
del lista[0]
print lista # ['bar', 'baz']
The difference between pop
and del
is that pop
returns the removed value, while del
only removes.
See an example:
lista1 = ['foo', 'bar', 'baz']
lista2 = ['aaa', 'bbb', 'ccc', 'ddd']
del lista1[0]
deleted = lista2.pop(0)
print (lista1) # ['bar', 'baz']
print (lista2) # ['bbb', 'ccc', 'ddd']
print ("O valor %s foi deletado da lista1" % deleted) # O valor aaa foi deletado da lista1
DEMO
There is also the remove()
method that, instead of specifying the index, is used to remove it from the list.
lista = ['foo', 'bar', 'baz']
lista.remove('foo')
print lista # ['bar', 'baz']
The remove
method will remove the first matching value, assuming that the list has two equal values, only the first value will be removed.