andylei andylei - 11 months ago 65
Python Question

Tips for debugging list comprehensions?

Python list comprehensions are nice, but near impossible to debug. You guys have any good tips / tools for debugging them?

Answer Source

If it's complicated enough that it's not obvious at first glance, unpack it into multiple steps and/or for loops. It's clearly too complicated, and making it more explicit is the easiest way to go about debugging it. Added bonus: you can now step through with the debugger or add print statements!