0
Can someone explain why value of x and y not same?
# Python code a = [1,2,3,4,1] for x in a: print(x) print("\n") for y in a: print(y) a[y] = 0 # output 1 2 3 4 1 1 0 3 0 1
6 Respostas
+ 1
For x you can directly understand that, there it iterates over list and prints element
now for y you are replacing element after printing.
First a[1] = 0(as y is 1 for the first iteration) this changes index one element to 0 so you can understand for every element
+ 1
You tagged three languages: python, c++, java. Only one is relevant.
+ 1
The reason why the output is different is that you change a inside the second loop.
0
Get rid of the irrelevant tags.
0
Simon Sauter which tag? Code is running without error.
0
Got the answer. Changing the list inside loop and iterating over same list. Due to which list next index data of the iterating list is getting modified.