+ 6

For loop question!

Sorry but I don't understand the result in this code: https://code.sololearn.com/ci80iNJVfn8r/?ref=app Why?

5th Apr 2022, 2:50 PM
Raffaele Bisogno
Raffaele Bisogno - avatar
14 Réponses
+ 6
Raffaele Bisogno You are using a reference data type variable as for temporary variable location which exist after the loop also so it retaining it's value after loop which is changed in loop. instead a Normal temporary variable will get garbage collected after the loop. hope it make sence.......!! edit: @Raffaele Bisogno yes. you are right.
5th Apr 2022, 4:34 PM
Jayakrishna 🇮🇳
+ 6
Raffaele Bisogno , if you wanted to see what happens, you can use following link to move through the execution just step by step. there is no magic or error in the code! on the left side you can see the code, also an indicator what line will be executed next. use navigation buttons on the right side you can see how the variable 'i' changes, as well as the list. both are always showing the current numbers don't worry about this link, it contains already the code that should be executed: https://pythontutor.com/visualize.html#code=a%3D%5B0,1,2,3%5D%0Afor%20i%20in%20range%28len%28a%29%29%3A%0A%20%20%20%20for%20a%5Bi%5D%20in%20a%3A%0A%20%20%20%20%20%20%20%20print%28a%5Bi%5D%29%0A%20%20%20%20print%28a%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false
5th Apr 2022, 8:10 PM
Lothar
Lothar - avatar
+ 5
Jayakrishna🇮🇳 If I understand, this could be the reasoning: If a = [0,1,2,3] and i is 0 the "for loop" does this: a[0] = 0 (the first element of a), print "0" a[0] = 1 (the second element of a), print "1" a[0] = 2 (the third element of a), print "2" a[0] = 3 (the last element of a), print "3" For each passage one element of the list a, in the order of the list, is re-assigned to a[0], but for the last passage a[0] retain the last element of the "for loop" ("3" in this case) so in the end the list changes. The List changes for each passage. Do I understand?
5th Apr 2022, 6:06 PM
Raffaele Bisogno
Raffaele Bisogno - avatar
+ 4
Erik sorry but I explained the question when I said that the "for loop" changed the list and this is not an unusual behaviour, as Orisa notices (see above). Actually the answers were coherent with my question, and very useful (I hope for all)!! I understood that the forum could be used even for general issues, like here: https://www.sololearn.com/Discuss/3012993/?ref=app
6th Apr 2022, 11:37 AM
Raffaele Bisogno
Raffaele Bisogno - avatar
+ 2
Orisa sorry but this is not the problem. If you run the code, you can see that in the first case the "for loop" changes the list. Why?
5th Apr 2022, 3:28 PM
Raffaele Bisogno
Raffaele Bisogno - avatar
+ 2
Thanks very much! This explains all!
5th Apr 2022, 6:10 PM
Raffaele Bisogno
Raffaele Bisogno - avatar
+ 2
Erik Ah, ok! I haven't considered this point, thank you!
7th Apr 2022, 1:40 PM
Raffaele Bisogno
Raffaele Bisogno - avatar
+ 1
for i in a and for i in range(len(a)) basically does the same thing in your code. But it's generally not the case. range(len(a)) is equal to range(4) as 4 elements are in your list. So, for i in range(len(a)) loops 4 times in the block below the loop. for i in a loops in the block below for each element in the list. Instead of being equal to a simple index like 0, 1, 2, 3 as seen before, i is equal to each value that is in the list. In your case, it's funny because it's also 0, then 1, then 2, then 3... The reason why you are confused is certainly because each element in the list has its value equal to its index.
5th Apr 2022, 3:25 PM
Orisa
Orisa - avatar
+ 1
Oh, you're right. That's some strange behavior there. I wonder if a[i] is considered as a variable itself or as an integer. But I don't think that it is the second option because it would throw an error if so. The result you're getting might be due to a memory error.
5th Apr 2022, 3:38 PM
Orisa
Orisa - avatar
+ 1
Yes. 👍 You're welcome..
5th Apr 2022, 6:11 PM
Jayakrishna 🇮🇳
+ 1
Lothar very very interesting!!! Thank you very much!!!
5th Apr 2022, 8:26 PM
Raffaele Bisogno
Raffaele Bisogno - avatar
+ 1
Lothar interesting site, thanks for that. Raffaele Bisogno in the future it would be better to ask an actual question...rather than "why does my code do this". (Do what? What expectation was not met? What did you already do to try to understand? Etc.)
6th Apr 2022, 11:15 AM
Erik
+ 1
Raffaele Bisogno I agree this was an interesting case, worth asking about and getting answered for all to see. You also did explain your question perfectly well in your first followup post -- just not in the originally posted question, which was my point. I think many people may view these threads sorted by popularity not by date, which causes a clarification post to be obscured, so it's better to put all the info up front. Anyway thanks for sharing, and happy coding to you!
7th Apr 2022, 11:56 AM
Erik
+ 1
I am now with programming and I start computer science I don't know how to do and I live in the place where we don't have any technology(in Afghanistan ) If anyone here wanna help with me, please send the message on this WhatsApp number 0093797580079
7th Apr 2022, 1:45 PM
Omair Ahmadzai
Omair Ahmadzai - avatar