0

Can someone please help me understand using outside variables in for loops

fruits = ["apples","cherries"] for fruits[-1] in fruits: print(fruits[-1], end ="|") Why is the output apples | apples |? Doesn’t fruits[-1] mean it will give an output of cherries | cherries |

22nd Sep 2021, 8:58 PM
Jenkins
1 Answer
+ 2
on first iteration, apples is assigned to fruits[-1] . List looks like this now : ["apples", "apples"] that is why fruits[-1] prints apples. ________________________________ on second iteration , apples is assigned to fruits[-1], and again fruits[-1] prints apples .
22nd Sep 2021, 9:20 PM
Abhay
Abhay - avatar