I've seen some Python list comprehensions before, but can this be done in a single line of Python?
errs = {}
for f in form:
if f.errors:
errs[f.auto_id] = f.errors
I've seen some Python list comprehensions before, but can this be done in a single line of Python?
errs = {}
for f in form:
if f.errors:
errs[f.auto_id] = f.errors
It probably could be, but as per the “Readability counts.” rule (PEP 20), I'd say it's a bad idea. :)
On the other hand you have “Flat is better than nested.” and “Sparse is better than dense.”, so I guess it's a matter of taste :)
Both ways are quite readable, however you should think of future maintainers of the code. Sometimes explicit is better. List comprehensions rule though :)