Speaking of list-comprehension?

George Sakkis gsakkis at rutgers.edu
Fri Jul 1 01:50:59 EDT 2005


"Terry Hancock" wrote:

> 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
>
> It's not exactly the same in that it doesn't change values in
> place, but this is similar if you are only interested in the
> values:
>
> ta = [t>=10 and t-10 or t+10 for t in ta]

Not quite; it differs for t==10.

George




More information about the Python-list mailing list