extract elements of n char from a list

Terry Reedy tjreedy at udel.edu
Fri Jul 26 11:54:17 EDT 2002


"Shagshag13" <shagshag13 at yahoo.fr> wrote in message
news:ahrchl$v4n9l$1 at ID-146704.news.dfncis.de...
> i want to efficiently extract elements of n char from a list :
...
> 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...)

You are looking for
pass_list = filter(predicate, candidate_list) # equivalent and maybe
faster (test) to
pass_list = [item for item in candidate_list if predicate(item)]

>>> l = ['this', 'is', 'an', 'example']
>>> nl=filter(lambda i: len(i) == 2, l)
>>> nl
['is', 'an']

Terry J. Reedy






More information about the Python-list mailing list