# removes item with given name in list
list = [15, 79, 709, "Back to your IDE"]
list.remove("Back to your IDE")
# removes last item in list
list.pop()
# pop() also works with an index...
list.pop(0)
# ...and returns also the "popped" item
item = list.pop()
# Basic syntax:
my_list.remove(element) # or:
my_list.pop(index)
# Note, .remove(element) removes the first matching element it finds in
# the list.
# Example usage:
animals = ['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit']
animals.remove('rabbit')
print(animals)
--> ['cat', 'dog', 'guinea pig', 'rabbit']
# Note only the first instance of rabbit was removed from the list.
# Note, if you want to remove all instances of an element (and it's the only
# duplicated element), you could convert the list to a set then back to a
# list, and then run .remove(element) E.g.:
animals = list(set['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit'])
animals.remove('rabbit')
print(animals)
--> ['cat', 'dog', 'guinea pig']
# removes item with given name in list
list = [15, 79, 709, "Back to your IDE"]
list.remove("Back to your IDE")
# removes last item in list
list.pop()
# pop() also works with an index..
list.pop(0)
# ...and returns also the "popped" item
item = list.pop()
# Deleting list items
my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']
# delete one item
del my_list[2]
print(my_list)
# delete multiple items
del my_list[1:5]
print(my_list)
# delete the entire list
del my_list
# Error: List not defined
print(my_list)
list_ = ["lots", "of", "items", "in", "a", "list"]
# Remove an item by index and get its value: pop()
>>> list_.pop(0)
'lots'
>>> list_
["of", "items", "in", "a", "list"]
# Remove an item by value: remove()
>>> list_.remove("in")
>>> list_
["of", "items", "a", "list"]
# Remove items by index or slice: del
>>> del list_[1]
>>> list_
['of', 'a', 'list']
# Remove all items: clear()
>>> list_.clear()
>>> list_
[]
a = ['apple', 'carrot', 'lemon']
b = ['pineapple', 'apple', 'tomato']
# This gives us: new_list = ['carrot' , 'lemon']
new_list = [fruit for fruit in a if fruit not in b]
# Remove from List
[1,2,3].pop() # 3 --> mutates original list, default index in the pop method is -1 (the last item)
[1,2,3].pop(1) # 2 --> mutates original list
[1,2,3].remove(2)# None --> [1,3] Removes first occurrence of item or raises ValueError.
[1,2,3].clear() # None --> mutates original list and removes all items: []
del [1,2,3][0] #