delete a list of elements from the list python code example

Example 1: delete element of a list from another list python

l1 = ["a", "b", "c", "d", "e", "f"]
l2 = ["b", "c", "e"]

l1 = [elt for elt in l1 if elt not in l2]
# l1 = ['a', 'd', 'f']

Example 2: remove element from list python

# Example 1:
# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']
# 'rabbit' is removed
animals.remove('rabbit')
# Updated animals List
print('Updated animals list: ', animals)


# Example 2:
# animals list
animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
# 'dog' is removed
animals.remove('dog')
# Updated animals list
print('Updated animals list: ', animals)

# Example 3:
# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']
# Deleting 'fish' element
animals.remove('fish')
# Updated animals List
print('Updated animals list: ', animals)