How can this be written on one line?

I have seen some views of the Python list before, but can this be done on a single line in Python?

errs = {}
for f in form:
    if f.errors:
        errs[f.auto_id] = f.errors
+3
source share
4 answers
errs = dict((f.auto_id, f.errors) for f in form if f.errors)
+20
source

Python 3.0 has a clear understanding of the shorter / more readable form of anser provided by Steef:

errs = {f.auto_id: f.errors for f in form if f.errors}
+9
source

, , " ". (PEP 20), , .:)

, " , ". " ", , :)

+4

, . . :)

0

Source: https://habr.com/ru/post/1710461/


All Articles