# sub-list extraction, logical indexation

Vlastimil Brom vlastimil.brom at gmail.com
Mon Aug 17 13:58:40 CEST 2009

```2009/8/17 MRAB <python at mrabarnett.plus.com>:
> Diez B. Roggisch wrote:
>>
>> Pierre wrote:
>>
>>> Hello Everyone,
>>>
>>> I would like to know if it is possible to extract a sub-list from a
>>> list ?
>>>
>>> typically if :
>>>
>>> L =[[1, 2, 3],[4, 5, 6],[3] ]
>>>
>>> How to extract easily the elements 0 and 2 of L in order to get :
>>>
>>> L2 =[[1, 2, 3],[3] ]
>>>
>>> Moreover, I would like to know if it is possible to use logical
>>> indexation such that :
>>>
>>> index = [ True, False, True]
>>> L2 = L
>>>
>>> or usual indexation, something like
>>> index=[0, 2]
>>> L2 = L
>>>
>>> I tried, but failed...
>>
>>
>> index = [ True, False, True]
>> L2 = [v for i, v in enumerate(L) if index[i]]
>>
> Or:
>
> L =[[1, 2, 3],[4, 5, 6],[3] ]
> index = [ True, False, True]
>
> from itertools import izip
> L2 = [v for i, v in izip(index, L) if i]
>
Or in python 3.1

>>> import itertools
>>> list(itertools.compress(data=[[1, 2, 3], [4, 5, 6], [3]], selectors=[True, False, True]))
[[1, 2, 3], [3]]

vbr

```