Remove Items from a List in Python (Pop, Del, Remove, Clear)

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Remove Items from a List in Python (Pop, Del, Remove, Clear)

There are several ways to remove items from a list: either by its index, its value, or the last item in your list, and even remove all of them – at once.

○ Remove an Item by Index (Pop, Del)

If you know the index of the item you want, you can use the pop() method to remove an item, at a given index. It will: modify the list and returns the removed item. If there is no index specified, pop() removes and returns the last item in the list. This may help you implement lists as stacks – meaning first in, last out data structure.

     my_list = ['red', 'green', 'blue']
     item = my_list.pop(1)
     
     # my_list = ['red', 'blue']
     # item = green

If you do not need the removed value, you can delete one or more items from a list, using this specific keyword del.

     my_list = ['red', 'green', 'blue']
     del my_list[1]
     
     # my_list = ['red', 'blue']

To remove more than one items, use the del keyword with a slice index.

     my_list = ['red', 'green', 'blue', 'yellow', 'black']
     del my_list[1:4]
     
     # my_list = ['red', 'black']



○ Remove an Item by Value (Remove)

If you are not sure where the item is in the list, you can use the remove() method to delete the given item, by value.

     my_list = ['red', 'green', 'blue']
     my_list.remove('red')
     
     # my_list = ['green', 'blue']

But, keep in mind that if more than one instance of the given item is present, this method removes only the 1st instance.

     my_list = ['red', 'green', 'blue', 'red']
     my_list.remove('red')
     
     # my_list = ['green', 'blue', 'red']



○ Remove ALL Items from a List (Clear)

You can also use the clear() method, to empty a list. Use clear() method to remove all items from the list, at once. And the del keyword to delete the list itself, entirely.

     my_list = ['red', 'green', 'blue']
     my_list.clear()
     
     # my_list = []

Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap