1

I have a list comprehension with a lambda like so, where i add the word to the list if its length is more than one character:

   dict['Company {}'.format(counter)]=[lambda x: len(x)>1 for x in sent[0].split()]

But it outputs a function instead of a list:

Company 13 [<function get_maps1.<locals>.<listcomp>.<lambda> at 0x000001F18E4191E0>, <function get_maps1.<locals>.<listcomp>.<lambda> at 0x000001F18E419268>, <function get_maps1.<locals>.<listcomp>.<lambda> at 0x000001F18E4192F0>]

How can i print its contents?

1
  • 1
    No, the output is a list of functions. Why did you expect anything else? Note, these functions are probably not working the way you expet them to anyway, since x will refer to the last x in the loop, but that is another matter... Commented Mar 29, 2021 at 11:34

1 Answer 1

4

You're getting a list of lambdas, because that's exactly what you are doing with this line of code:

[lambda x: len(x)>1 for x in sent[0].split()]

What you probably intended to do is this instead:

[len(x)>1 for x in sent[0].split()]

which results in a list of booleans

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

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.