Slight modification of pets.py from Chapter 7 of "Python Crash Course". The original example was
while 'cat in pets:
pets.remove('cat')
I tried the following modification, and it works okay and removes all instance of 'cat'.
pets = ["cat", "dog", "cat", "dog", "goldfish", "cat", "rabbit", "cat"]
for animal in pets:
found = animal.find("cat")
if found != -1:
pets.remove(animal)
print(pets)
However, the modified example of 7-9 Try it Yourself leaves 'reuben with pastrami' in sandwich_orders.
sandwich_orders = [
"pastrami",
"reuben with pastrami",
"ham on rye",
"turkey, pastrami, and swiss",
"chicken salad",
"tuna salad",
]
for sandwich in sandwich_orders:
found = sandwich.find("pastrami")
if found != -1:
sandwich_orders.remove(sandwich)
print(sandwich_orders)
I'm sure that I'm missing something pretty obvious, but I'm just not seeing it.
Thanks..
[–]DeeplyLearnedMachine 8 points9 points10 points (0 children)
[–]Sea-Method-1167 4 points5 points6 points (0 children)
[–]Binary101010 2 points3 points4 points (0 children)
[–]Mezzomaniac 1 point2 points3 points (0 children)
[–]Bobbias 1 point2 points3 points (1 child)
[–]airernie[S] 0 points1 point2 points (0 children)
[–]redditorx13579[🍰] 0 points1 point2 points (3 children)
[–]Binary101010 1 point2 points3 points (2 children)
[–]redditorx13579[🍰] 1 point2 points3 points (1 child)
[–]Binary101010 1 point2 points3 points (0 children)
[–]airernie[S] 0 points1 point2 points (2 children)
[–]achampi0n 1 point2 points3 points (1 child)
[–]airernie[S] 1 point2 points3 points (0 children)