Shouldn’t it had returned all the numbers that were divisible by 10? Which would have been 20, 30, and 40 but instead it printed out 20 and stopped. Why is that?
@java2115950072, don’t forget what return does:
- it returns a value to the calling statement
- It halts processing of the function.
Once return is reached one time, the function halts.
In addition to what @patrickd314 says, to obtain a list based on a list, you generally want to create an entirely new list rather than modifying the input. Why? It’s simpler, it leaves the original intact for future use, and it doesn’t really cost you anything.
You especially wouldn’t want to move values around (which you do by deleting elements) while you’re iterating through it, because that interferes with the iteration (you’re kind of pulling the rug out from under the loop)
Thank you for this. Will keep it in mind next time