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:
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:
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.
What being said, for x in (i in vec if i > 3) does the job, as well as itertools.ifilter (or just the builtin filter in Python 3) with a lambda x: x > 3 predicate. They work with all iterables as well, and are lazy (e.g. if you break during the loop, you didn't check a single item too much).
It can, but list comprehensions/generator expressions are the exact sort of thing that should be used here. Depending on what you want to do in your if block, you could use some form of map or reduce, but list comprehensions and generator expressions are likely the best way to do it.
You can imagine somethings like this :
No, you can't. The Python language reference states:
Indeed, Python generates a
SyntaxError
for the nested ifs above. More formally regardingfor
, this is its grammar in Python:Note that when
for
is followed by a statement without an indentation, that statement must be astmt_list
, which only allowssimple_stmt
instances.simple_stmt
is this:Which doesn't include compound statements like
if
andfor
.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.
See @KennyTM... no reason to compress that much.
What being said,
for x in (i in vec if i > 3)
does the job, as well asitertools.ifilter
(or just the builtinfilter
in Python 3) with alambda x: x > 3
predicate. They work with all iterables as well, and are lazy (e.g. if youbreak
during the loop, you didn't check a single item too much).It can, but list comprehensions/generator expressions are the exact sort of thing that should be used here. Depending on what you want to do in your
if
block, you could use some form ofmap
orreduce
, but list comprehensions and generator expressions are likely the best way to do it.Yes
for x in filter(lambda i:i>3,vec):