I do not understand why my 'old' list changes even though they do not share the same ID. I have not been able to find out, when I debug, I see the old list change, but cannot find a way around this. Found that creating a new list with a slice should change the ID of the two, but alas, here I am.
The only part of the code that actually uses the old list, is when I use .format.(old[i])
, any help is appreciated.
menu = [
["egg", "bacon"],
["egg", "sausage", "bacon"],
["egg", "spam"],
["egg", "bacon", "spam"],
["egg", "bacon", "sausage", "spam"],
["spam", "bacon", "sausage", "spam"],
["spam", "sausage", "spam", "bacon", "spam", "tomato", "spam"],
["spam", "egg", "spam", "spam", "bacon", "spam"],
]
old = list(menu)
for i in range(len(menu))[::-1]:
if "spam" in menu[i]:
for k in range(len(menu[i]))[::-1]:
if "spam" == menu[i][k]:
del menu[i][k:k+1]
for i, meal in enumerate(menu):
print("-"*80)
print("{} contains, without spam:\n".format(old[i]))
for part in meal:
print(part)
else:
print("-"*80)
Small shoutout to a Python Masterclass, and Monty Python...