Can anybody explain what’s going on here | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anybody explain what’s going on here

g=["h","a","y","k"] y=[i=="a" for i in g if g.remove(i)] print(g) It returns [“a”,”k”] WHY?

4th Mar 2023, 12:58 PM
Hayk Hakobyan
Hayk Hakobyan - avatar
12 Answers
+ 8
After the removing of an item the index of the for loop stay the same. That means after that the pointer is one step further because now the first item is the further item. So the „a“ is first but the pointer is now at the second item etc. Besides the code can be reduced to the same as follows (of course without the print statements in the for loop): g=["h","a","y","k"] for i in g: print('i = ',i) print('g = ',g) g.remove(i) print('after remove g = ',g) print() print(g)
4th Mar 2023, 1:58 PM
JaScript
JaScript - avatar
+ 7
One good lesson learned from this post is ... now we know we should not alter a data container while iterating it. Good question! I just wanted to remind you to add language specifics in the post tags, for relevance sake : )
4th Mar 2023, 3:15 PM
Ipang
+ 3
You are removing "h", "y" from list g. first time it removes g.remove("h") So list modifies to [ "a", " y", "k"] So next item is "y" by loop. So it is removed and list is ["a", "k"] and no next() item by loop. What are your expectations..? Is it clear ?
4th Mar 2023, 1:46 PM
Jayakrishna 🇮🇳
+ 2
Your code does not make any sense. Firstly, you make a new variable "y" with some arguments, but then you try to pring the variable "g" (for no reason). Secondly, the condition inside the definition of "y" is weird: do you really want to try if a letter is equal to letter "a" for i in g? If your only question is why it outputs ["a","k"] after printing g: you remove the first item from the list by that "g.remove(i)", so that is the letter "h". Then you apply the condition to the new first letter, which is "a", so it doesn't get deleted. After that, the process repeats, "y" gets removed and "k" stays.. Your code is really confusing. I do not know what you are trying to achieve by that. Hope that I helped you somehow!
4th Mar 2023, 1:42 PM
Paleon
Paleon - avatar
+ 2
My question is first i is “h” so it removes h, then i is “a” it must remove a, then “y” then “k”, why it doesnt remove “a” and “k”
4th Mar 2023, 1:51 PM
Hayk Hakobyan
Hayk Hakobyan - avatar
+ 2
["h", a", "y", "k"] Initially.. If you remove "h" Then list becomes ["a", "y", "k"] So here next(g) item by loop is at index 1, not again from 0. And which is "y", not "a", so it is removed. List becomes ["a", "k"] and no next () is picked by loop because index 2 is out of range now in list g = [ "a", "k"] . So these two items are skipping by process. Try g.copy() instead of g in loop then all items removed. Because you will work on different list. Not same list concurrently....
4th Mar 2023, 2:03 PM
Jayakrishna 🇮🇳
+ 2
At first iteration, item is fetched at index 0, and in next iteration, item is fetched at index 1, and next at index 2 next at index 3 ...
4th Mar 2023, 2:10 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna ,why next(g) item by loop is at index 1,instead of index 0?
4th Mar 2023, 2:07 PM
Hayk Hakobyan
Hayk Hakobyan - avatar
+ 1
JaScript thank you it helped understand whats going on but i still dont understand why after removing h it removes y not a
4th Mar 2023, 2:08 PM
Hayk Hakobyan
Hayk Hakobyan - avatar
+ 1
when i print g=[“h”,”a”,”y”,”k”] for i in g: print(i) h a y k
4th Mar 2023, 2:10 PM
Hayk Hakobyan
Hayk Hakobyan - avatar
+ 1
Oh i think i got it Jayakrishna thank You so much
4th Mar 2023, 2:11 PM
Hayk Hakobyan
Hayk Hakobyan - avatar
+ 1
Thank you everyone so much you helped me understand!!!
4th Mar 2023, 2:12 PM
Hayk Hakobyan
Hayk Hakobyan - avatar