Speaking of list-comprehension?

Steven Bethard steven.bethard at gmail.com
Fri Jul 1 01:08:49 EDT 2005


Chinook wrote:
>  >>> ta = [5, 15, 12, 10, 9]
>  >>> for i in range(len(ta)):
> ...   if ta[i] >= 10:
> ...     ta[i] -= 10
> ...   else:
> ...     ta[i] += 10
> ...
>  >>> ta
> [15, 5, 2, 0, 19]

One possibility:

py> [(tai + 10, tai - 10)[tai >= 10] for tai in ta]
[15, 5, 2, 0, 19]

But see:

http://www.python.org/peps/pep-0308.html

STeVe



More information about the Python-list mailing list