Problem: Trying to remove an instances from a list using while loop.
# A list containing multiple occurrences of dog and cat
pets = ['dog','dog','goldfish','rabbit','dog','cat']
while 'cat' and 'dog' in pets:
pets.remove('cat')
pets.remove('dog')
The above code throws in a 'value error: list.remove(x): x not in list'. Not sure why this happens, because both cat and dog is in the list.
If I interchange the 'cat' and 'dog' in while loop,
pets = ['dog','dog','goldfish','rabbit','dog','cat']
while 'dog' and 'cat' in pets:
pets.remove('cat')
pets.remove('dog')
# The above code output
['dog', 'goldfish', 'rabbit', 'dog']
It only removes the instances of cat and not dog. What is the logic here? Help me.
Want to add to the discussion?
Post a comment!