can this be written in one line without List Comprehensions?
for x in vec:
if x > 3:
...
...
No, you can't. The Python language reference states:
Compound statements consist of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn’t be clear to which if clause a following else clause would belong:
if test1: if test2: print x
Indeed, Python generates a SyntaxError
for the nested ifs above. More formally regarding for
, this is its grammar in Python:
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
Note that when for
is followed by a statement without an indentation, that statement must be a stmt_list
, which only allows simple_stmt
instances. simple_stmt
is this:
simple_stmt ::= expression_stmt
| assert_stmt
| assignment_stmt
| augmented_assignment_stmt
| pass_stmt
| del_stmt
| print_stmt
| return_stmt
| yield_stmt
| raise_stmt
| break_stmt
| continue_stmt
| import_stmt
| global_stmt
| exec_stmt
Which doesn't include compound statements like if
and for
.
That said, keep in mind that Python's syntax is aimed at clarity. Therefore it's better not to nest such statements, this is what generators/list comprehensions were made for. If you deem your computation to be simple enough for a single line, then comprehensions are for you. Otherwise, you really don't want to clutter the code by having everything on a single line - break it up nicely with indentation. A few extra lines don't cost much these days.