The method can also be used del to remove an item by specifying its index.
lista = ['foo', 'bar', 'baz']
del lista[0]
print lista # ['bar', 'baz']
The difference between pop and del is that pop returns the value removed, while del only remove.
Take 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 method remove() that, instead of specifying the index, the value is used to remove it from the list.
lista = ['foo', 'bar', 'baz']
lista.remove('foo')
print lista # ['bar', 'baz']
The method remove will remove the first corresponding value, assuming that the list has two equal values, only the first will be removed.
In the case,
listahas the first element removed andlista.pop(0)returns the element that has been removed?– Wallace Maxters
This, if you only use
lista.pop()remove and return the last item from the list. @Wallacemaxters– Lucio Rubens