+ 1

Why will "loop" be printed 3 times and not 6 times?

This was one question in python challenge: "How many times will the string "loop" be printed?" a = [0, 1, 2, 3, 4, 5] n = 0 for _ in a del(a[n]) n += 1 print("loop") I didn't know what the "_" operator does, so I tried to google it and I found this article: https://hackernoon.com/understanding-the-underscore-of-python-309d1a029edc Apparently, the underscore is used to ignore the index but how does that change the number of iterations through the loop?

16th May 2019, 6:01 AM
markus
1 Antwort
+ 2
"_" is "strange" name of variable, it could be any letter, or word here. then - normally this for loop would run 6 times, but each time you iterate through it you delete 1 value from a[], so after 3 passes, you have 3 values left and for loop run 3 times already, so program ends.
16th May 2019, 6:37 AM
domind
domind - avatar