sub-list extraction, logical indexation

Diez B. Roggisch deets at nospam.web.de
Mon Aug 17 06:13:31 EDT 2009


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]]

Diez



More information about the Python-list mailing list