For-if without list comprehension in one line

can this be written on one line without List?

for x in vec: if x > 3: ... ... 
+7
python list-comprehension
source share
5 answers

No, you can’t . The Python language reference reads:

Compound statements consist of one or more. "A sentence consists of a heading and a set. Article headings for a particular compound expression are still indented. Each sentence heading begins with a unique keyword identification and ends with a colon. A suite is a group of statements controlled by an item. A set can be one or more semicolon-separated simple statements on the same line as the heading, after the colon headers, or there may be one or more indentation . on subsequent lines in only the last set of the form can contain nested compound statements: illegally, mainly because it was not clear to which if the else clause would belong:

 if test1: if test2: print x 

Indeed, Python throws a SyntaxError for the nested ifs above. More formally relative to for , this is his 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 for follows an stmt_list statement; this statement must be stmt_list , which allows only instances of simple_stmt . simple_stmt :

 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 does not contain compound statements such as if and for .


However, keep in mind that Python syntax is aimed at clarity. Therefore, it is better not to embed such statements, this is what was done to understand generators / lists. If you think your calculations are simple enough for one line, then there is an understanding for you. Otherwise, you really do not want to clutter up the code, having everything in one line - perfectly open it with indentation. A few extra lines these days are not so expensive.

+3
source share

See @KennyTM ... there is no reason to squeeze heavily.

As said, for x in (i in vec if i > 3) does the job, as well as itertools.ifilter (or just the built-in filter in Python 3) with the predicate lambda x: x > 3 . They also work with all iterators and are lazy (for example, if you break during a loop, you did not check one element too much).

+3
source share

It can, but a list of expression expressions / generators is what you need to use here. Depending on what you want to do in your if block, you can use some form of map or reduce , but probably the best way to do this is with lists and generator expressions.

0
source share

Yes

for x in the filter (lambda i: i> 3, vec):

0
source share

You can imagine things like this:

 def do_something(value): ... def do_otherthing(value): ... for x in t: do_something(x) if x>3 else do_otherthing(x) 
-one
source share

All Articles