extract elements of n char from a list

Shagshag13 shagshag13 at yahoo.fr
Fri Jul 26 07:48:23 EDT 2002


"Shagshag13" <shagshag13 at yahoo.fr> a écrit dans le message de news: ahrchl$v4n9l$1 at ID-146704.news.dfncis.de...
> hello,
>
> i want to efficiently extract elements of n char from a list :
>
> n = 2
> l = ['this', 'is', 'an', 'example']
> nl = []
> for i in l:
>     if len(i) == n:
>         nl.append(i)
>
> nl = ['is', 'an']
>
> but i think that some clever guru could use a map() / reduce() or something like that to speed up the process (which is what i
really need...)

well, i think that i could use :

nl = [e for e in l if len(e) ==n]

but is it efficient ? are they more efficient ways ? and when is it good pythoner technique to use list comprehensions ?

thanks,

s13.





More information about the Python-list mailing list