[Python-ideas] Calling a function of a list without accumulating results
Ron Adam
rrr at ronadam.com
Thu Sep 27 06:54:21 CEST 2007
Terry Jones wrote:
> OK, how about "lack of choice"? :-)
There's always a choice... Not always a good one though. ;-)
> but you don't have a (simple) choice if you don't want to accumulate
> results. I'm merely saying that I think it would be cleaner and more
> consistent to allow
>
> print(x) for x in range(5) if x % 2 == 0
>
> instead of having the non-choice but to write something like
>
> for x in range(5):
> if x % 2 == 0:
> print x
>
>>> a = list(range(10))
>>> def pr(obj):
... print obj
...
>>> a = list(range(10))
>>> b = [1 for x in a if pr(x)]
0
1
2
3
4
5
6
7
8
9
>>> b
[]
Or to be more specific to the above example...
>>> b = [1 for x in range(5) if x % 2 == 0 and pr(x)]
0
2
4
>>> b
[]
All of which are more complex then a simple for loop.
Cheers,
Ron
More information about the Python-ideas
mailing list