Remove Items from a List in Python (Pop, Del, Remove, Clear) - Python Tutorial for Beginners

Publié le: 14 mars 2021
sur la chaîne: Digital Academy
9,710
214

🎓 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 -    • DATA TYPES in Python (Numbers, String...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assi...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF,...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Co...  

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

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

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

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#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/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

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

***


Sur cette page du site, vous pouvez voir la vidéo en ligne Remove Items from a List in Python (Pop, Del, Remove, Clear) - Python Tutorial for Beginners durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Digital Academy 14 mars 2021, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 9,710 fois et il a aimé 214 téléspectateurs. Bon visionnage!