pop() to remove item based on position in a list
pop(i) takes one argument as position ( optional ) .
i
: ( Position ) Position of the element to be removed ( 0 based index )
If no position is given ( input i ) then last element of the list is removed.
my_list=['a','b','c','d','e']
my_list.pop(2)
print(my_list)
Output ( element at position 2 , c is removed )
['a', 'b', 'd', 'e']
Using String
my_list=['Alex','Ronald','John']
my_list.pop() # deletes last element
print(my_list)
Output is here
['Alex', 'Ronald']
We can remove element from a particular position
my_list=['Alex','Ronald','John']
my_list.pop(0) # deletes first element
print(my_list)
Output is here
['Ronald', 'John']
We can collect the removed element
my_list=['Alex','Ronald','John']
name=my_list.pop(1) # deletes first element
print(name)
Output is here
Ronald
« All list methods « Questions with solutions on List « clear() to remove all items
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com