python for with double test

Alan Franzoni at
Fri Dec 30 18:40:23 CET 2005

Il 30 Dec 2005 09:02:30 -0800, borges2003xx at ha scritto:

> hi all
> the is a way for doing  a for with double test:

what's a 'double test' exactly? :-)

'for' does no test, it just iterates over a list. If you want to execute
the iteration only if f is 1, do this:

if f==1:
	for i in range(0,10):

if you want to use only certain values in the range, when the value of f[i]
(f may be a list, a dictionary, or whatever) is 1, you can use a list

for i in [x for x in range(0,10) if f[x]==1]:

of course, if you want to make it shorter, you can define a function
instead of the 'for' code block and use the list comprehension directly:

def myfunction(x):

[myfunction(x) for x in range(0,10) if f[x]==1]

and you'll end up with a list of the return values of myfunction.

Alan Franzoni < at>
Togli .xyz dalla mia email per contattarmi.
To contact me, remove .xyz from my email address.
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E

More information about the Python-list mailing list