example problem from a book
if i wanted to check through the list for pastrami
and make sure no pastrami ended up in finished
, this works fine:
orders = ['tuna sub', 'chicken parmasean', 'pastrami', 'chicken teryiaki', 'pastrami']
finished = []
for order in orders:
while 'pastrami' in orders:
orders.remove('pastrami')
print("preparing " + order + " ...")
finished.append(order)
for sandwich in finished:
print(sandwich + " is ready")
└─$ python3 sandwiches.py
preparing tuna sub ...
preparing chicken parmasean ...
preparing chicken teryiaki ...
tuna sub is ready
chicken parmasean is ready
chicken teryiaki is ready
but using if
to check order
does not work.
orders = ['tuna sub', 'chicken parmasean', 'pastrami', 'chicken teryiaki', 'pastrami']
finished = []
for order in orders:
if order == 'pastrami':
orders.remove('pastrami')
print("preparing " + order + " ...")
finished.append(order)
for sandwich in finished:
print(sandwich + " is ready")
─$ python3 sandwiches.py
preparing tuna sub ...
preparing chicken parmasean ...
preparing pastrami ...
preparing pastrami ...
tuna sub is ready
chicken parmasean is ready
pastrami is ready
pastrami is ready
I don't understand why this happens?