foods = ["Apple", "Banana", "Pineapple", "Orange"]
removeFoods = foods[2]
for removeFoods in foods:
foods.remove(removeFoods)
print(foods)
foods = ["Apple", "Banana", "Pineapple", "Orange"]
removeFoods = foods[2]
if removeFoods in foods:
foods.remove(removeFoods)
print(foods)
Asking for the differences between those two functions (for and if), and why the outputs were different.
CodePudding user response:
In both these cases, removeFoods is a variable that's set to index 2 in the list, aka "Pineapple".
Let's look at your 'if' statement first.
if removeFoods in foods:
foods.remove(removeFoods)
In English, this line asks: "if the word Pineapple is in the list of foods, then remove Pineapple from the list." So when you print the list, you get what you'd expect: ["Apple", "Banana", "Orange"].
I think your 'for' line is what is causing the confusion.
for removeFoods in foods:
foods.remove(removeFoods)
This is a tricky line, because removeFoods here is actually not "Pineapples" anymore. As soon as you type 'for X in foods' in any program you write, your program creates a new variable, X, that represents each index in the list as you move through it. In other words, your program is creating a new variable called removeFoods that is not the same one you used before, even though they happen to share the same name. This removeFoods is set to "Apple" at first, and then "Banana", and then "Pineapple", and then "Orange".
Here is how your 'for' loop functions:
foods = ["Apple", "Banana", "Pineapple", "Orange"]
On the first iteration, removeFoods is set to index 0, which is "Apple". "Apple" is then removed from the list, which changes the list within the loop to equal ["Banana","Pineapple","Orange"]. Now, the program will move on to index 1, but the list has changed! Index 1 is no longer "Banana", it's "Pineapple". "Pineapple" will be removed from the list, leaving ["Banana","Orange"]. The program will now move on to index 2, but that doesn't exist anymore! So, it's done.
CodePudding user response:
First case: the explicit assignment to 'removeFoods' is irrelevant. The for-loop processes the first, second, ... elements of the 'foods' list.
First time through, processing first element of the list:
foods is ["Apple", "Banana", "Pineapple", "Orange"]
removeFoods is "Apple"
thus foods is changed to ["Banana", "Pineapple", "Orange"]
Second time through, processing second element of the list:
foods is ["Banana", "Pineapple", "Orange"]
removeFoods is "Pineapple"
thus foods is changed to ["Banana", "Orange"]
There is no third time, since the list (now) has two elements.
This is all strange because, as was noted in a comment, you're modifying the list you're iterating over, as you're iterating over it. This is generally not useful, so don't do it. I'd have to read the language spec to see if the resulting behaviour is formally undefined.
The second case is trivial: there's no loop, you're just removing "Banana" from 'foods'. You don't even need the 'if'.
CodePudding user response:
Because when you ask if removeFoods (food[2]
so pinapple), the answer is True, because pinapple is in foods, and it remove pinapple of foods, so the output is ['Apple', 'Banana', 'Orange']
. In the first code, the first time it loops, removeFoods = foods[0]
so 'Apple' and after it removes it so foods = ['Banana', 'Pineapple', 'Orange']
, and the second time it loops removeFoods = foods[1]
so 'Pineapple', cause foods doesn't have 'Apple' now, after it removes it, and foods = ['Banana', 'Orange']
.