I am currently working with the data structure list and am wondering how to remove all elements found in one list that occur in another list. I've seen a couple of examples on Stack Overflow that spoke about removing single elements from a list but not for instances with removing more than one types of identical elements (like the example below without manually removing each instance). For example, given the two lists below:
friends_pets = ['Chicken', 'Chicken' 'Dog', 'Pigeon', 'Dog', 'Cat', 'Cat', 'Cat']
personal_pets = ['Dog', 'Cat']
I want my function to return:
>>> ['Chicken', 'Chicken', 'Pigeon']
I figured that using the filter() method to return the desired list over remove() would seem most ideal, however, I'm having difficulty accessing the information that Python is storing at a specific address.
for pet in personal_pets:
filter(pet, friends_pets)
>>> <filter object at 0x10bfa2d90>
>>> <filter object at 0x10bfa2e50>
I even tried running:
for pet in personal_pets:
list(filter(pet, friends_pets))
however, it states that the 'str' object is not callable.
The closest I can get using remove() is:
for pet in personal_pets:
friends_pets.remove(pet)
>>> ['Chicken', 'Chicken', 'Pigeon', 'Dog', 'Cat', 'Cat', 'Cat']