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

Показать описание
🎓 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']
# 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 = ['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 = ['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 = []
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
🖥️ 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']
# 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 = ['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 = ['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 = []
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
Комментарии