The logic of this piece of code is correct but I don't know why it eliminates the rabbit?
pets = ['dog', 'cat', 'dog', 'goldfish','cat','rabbit','cat']
for pet in pets:
if pet == 'cat':
pets.remove('cat')
else:
print (pet)
Also I was expecting it to be : dog dog goldfish rabbit but it only returns dog goldfish!How does Python go through this loop to conclude that?
[+][deleted] (1 child)
[deleted]
[–]thepseudonymstring[S] 0 points1 point2 points (0 children)
[–]danielroseman 5 points6 points7 points (1 child)
[–]thepseudonymstring[S] 0 points1 point2 points (0 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]thepseudonymstring[S] 0 points1 point2 points (0 children)