How to remove elements from a list?

Опубликовано: 06 Декабрь 2024
на канале: CyberTodd
352
12

You can remove items from a list in multiple ways.

Example 1 - del list[index] - del: deletes the item at the index you provide

list = ['First Value', 'Second Value', 'Third Value']
del list[0]
print(list)

Output:
['Second Value', 'Third Value']

Example 2 - list.pop() - pop takes out the last item inside the list

list = ['First Value', 'Second Value', 'Third Value']
take_item = list.pop()

Output:
['First Value', 'Second Value']

NOTE: with the pop function you can specify inside the parenthesis the index of the item you want to delete also: list.pop(1) will result in ['First Value', 'Third Value'].

-------------------------Subscribe----------------------------
• If you like to see a variety of fun and interactive projects written in Python
• If you love ☕ and 🐶 WAIT! - Yeah! Definitely, coffee and dogs!
-----------------------------------------------------------------------

-------------------------Socials----------------------------
• Twitter:   / iftodd1  
• Facebook:   / iftodd  
• Instagram:   / if_todd  
------------------------------------------------------------------

-------------------------Support----------------------------
You can support me here:   / if_todd  
--------------------------------------------------------------------

How to remove elements from a list? #Python #shorts