0

The question is odds([1, 2, 3, 4, 5, 6]) the output must be [1, 3, 5]. I know its simple in list comprehension, or a small function.

def odds(l):
    r = []
    for n in l:
        if n % 2 == 1:
            r.append(n)
    return(r)```

or


    def odds(l):
        return [n for n in l if n % 2 == 1]

but i need the output using lambda function shown below

    odds = lambda :

3 Answers 3

2

odds = lambda l: [x for x in l if x % 2 == 1]

Without list expression:

odds = lambda l: list(filter(lambda x:x%2==1, l))

But list expressions are the more pythonic solution always.

Sign up to request clarification or add additional context in comments.

2 Comments

you are still using list comprehension, no list comprehension must be used.
you didn't wrote that. But well, then I would say: odds = lambda l: list(filter(lambda x:x%2==1, l))
1

This stores a lambda function object in ans.

ans = lambda l: [i for i in l if i&1 == 1]

To use the function:

ans([1,2,3,4,5,6,7])
Output:

[1, 3, 5, 7]

1 Comment

you are still using list comprehension, no list comprehension must be used.
0
odds = lambda _: list(filter(None, map(lambda x: x if x % 2 == 1 else None, _)))

Without using list comprehension I think there is no simpler way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.