DhKo
DhKo

Reputation: 292

Can I use lambda to accept two arguments in python and use it along with map

I want to write solve this in python a function a list of words and an integer n and returns the list of words that are longer than n i.e

retlist=list()
def retword(list,n):
    for i in list:
        if len(i)>=n:  
            retlist.append(i)   
return retlist     

I can easily do this using this function but I want to solve this using map, filter, reduce and lambda expressions. Something like this

map(lambda list,len:list[i] if len(list[i])> len, (list,len))

Upvotes: 0

Views: 81

Answers (2)

zipa
zipa

Reputation: 27869

List comprehensions will do the job you need if a is original list and n is the desired length:

retlist = [i for i in a if len(i) >= n]

Upvotes: 0

Daniel
Daniel

Reputation: 42758

Use filter:

filter(lambda s: len(s) >= n, list)

Upvotes: 2

Related Questions