3 ms·
Using python, the choice between a list comprehension or a for loop is often a style choice. I can't say this is common practice, but a list comprehension is g
by musingsole 6y ago
Using python, the choice between a list comprehension or a for loop is often a style choice.
I can't say this is common practice, but a list comprehension is great when the iteration is simple and quickly becomes hard for a human to parse if you're doing anything slightly complex. If you need any complex logic, you have to use a for-loop. So -- when written by a developer with these perspectives -- a for-loop signals complicated iteration logic and a list comprehension signals simple iteration logic. But you could do either with both if you chose. So, it's a style choice that can be used to convey meaning.
- jl2718 6y agoI failed a google interview for using a nested generator comprehension instead of for loops. It solved the problem, but the interviewer was utterly disgusted by it. He had some fair points about it being hard to inspect and modify and add exceptions.