+ 3

Understanding why this error behaves as it does

a = ['dog', 'train', 'royalty'] for x in a: if len(x) > 6: a.insert(0, "dragons") print(x) in this code I reached an endless loop and I understand that this is caused by me updating the list while the for loop is running the loop I did fix it by making a copy of a so that I would run on a elements but won't get stuck b = a[:] for x in b: if len(x) > 6: a.insert(0, "dragons") print(x) yet what leads to this question is I assumed initially that the for loop element x will begin from places zero the index will be reset and so I should have gotten dragons printed and stuck in an endless loop because of that but to my surprise I am getting royalty printed so in actuality it seems like since the loop is updated now there is an element in the last index to check but also the last element became royalty and so a keeps adding dragons to the start and royalty keeps getting printed the only things that I could come up with is that I am actually continuing from the last index to index +1 BUT SINCE I inserted an element to index 0 the previous last index is now the last I am checking and royalty is the one getting printed , so should I think of x though it is the element I have now in the list as element + index so following the logic of for i in range (Len(list)) and so i would not actually reset it would just cause the loop to continue from last index ?

26th Jul 2024, 4:17 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
10 Respuestas
+ 4
Congratulations, you figure it on your own. This link gives more details, maybe you will be interested in. https://inventwithpython.com/beyond/chapter8.html#calibre_link-172
27th Jul 2024, 1:27 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
I think you are correct. It does not start over, it continues where it left off, and since you inserted a new name, the indices have shifted. It sounds like you got it all figured out, well done.
26th Jul 2024, 6:51 PM
Chris Coder
Chris Coder - avatar
+ 1
Hi Idris Alim if you go under the discussion section there is a button to make a new post to get more exposure ,you can also ask your question here but it would not be as effective since i already accepted an answer,secondly when asking a question under another post there is a risk of you getting an answer and then it getting deleted if the poster deletes the original post
27th Jul 2024, 11:47 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
I think my question sums up to does python following syntax for x in a: print(x) actually get treated internally as follows for x in range(len(a)): print(a[x]) since then I can see how I should not assume it restarting from a[0] to a[1] to a[len(a)-1]
26th Jul 2024, 4:29 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
Chris Coder thank you i still have a long way to go , and your profile is such a blast ,its motivating to say the least
26th Jul 2024, 7:03 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
Dareen Moughrabi You're welcome and thank you too! I have not paid much attention to my profile 😅 . I'm happy you found it motivating. 😉
26th Jul 2024, 7:32 PM
Chris Coder
Chris Coder - avatar
0
Wong Hei Ming thank you , this was a fun read
27th Jul 2024, 3:25 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
and sorry all for the bad phrasing ,i have tried to edit the post, but it is not allowing me to, i have now noticed how the names of the variables are a bit confusing if they ain’t bolded , and that i wrote some things in a sense twice
27th Jul 2024, 3:29 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
Hi i need help
27th Jul 2024, 9:25 AM
Idris Alim
Idris Alim - avatar
0
Ok
27th Jul 2024, 9:16 PM
Idris Alim
Idris Alim - avatar