pop() to remove item based on position in a list

All list methods

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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer