Python list/array – remove an item

To remove an item from python list method pop([i]) can be used.

Remove last item

a = ["v1", "v2", "v3", "v4"]
item = a.pop()
print item
print a
v4
['v1', 'v2', 'v3']
Env: Python 2.7.6

Remove first item

a = ["v1", "v2", "v3", "v4"]
item = a.pop(0)
print item
print a
v1
['v2', 'v3', 'v4']
Env: Python 2.7.6

Remove ith item

a = ["v1", "v2", "v3", "v4"]
item = a.pop(2)
print item
print a
v3
['v1', 'v2', 'v4']
Env: Python 2.7.6
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments