sub-list extraction, logical indexation

MRAB python at mrabarnett.plus.com
Mon Aug 17 12:35:15 CEST 2009


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...
>> Thanks for your help
> 
> 
> 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]




More information about the Python-list mailing list