Partial list comprehensions

Mensanator mensanator at aol.com
Fri Dec 4 15:41:11 EST 2009


On Dec 4, 2:22 pm, Joel Davis <callmeclaud... at gmail.com> wrote:
> Is it possible to run a list comprehension over a certain portion of
> the list? My goals is to be able to run the comprehension on the
> innermost elements of the list, but leaving the outermost intact.

Something like this?

>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [i for i in a[0:5]]
>>> b
[0, 1, 2, 3, 4]
>>> c = [i for i in a if i%2==0]
>>> c
[0, 2, 4, 6, 8]
>>>



More information about the Python-list mailing list