1

There is a list:

x = [5, "ce", 0, (32, "a")]

It doesn't contain a None, or empty array, or False element, so it should return True.

0 shouldn't be counted as an empty object.

y = [5, "ce", 0,, "", (32, "a")]

It contains an empty string so it should return False.

How would you do it in the fastest way?

5
  • Are you concerned about this specific list, or any list in general that may contain zero, blank string, False values, empty sublists, etc? Commented Sep 30, 2019 at 13:45
  • 1
    I believe False == 0. Commented Sep 30, 2019 at 13:46
  • How about empty sequence, empty dict? Commented Sep 30, 2019 at 13:48
  • @JohnGordon I'm interested in any list in general. Commented Sep 30, 2019 at 14:22
  • @RomanPerekhrest the same, it should count as None. Commented Sep 30, 2019 at 14:23

1 Answer 1

2

Use the built-in all:

>>> all(e not in [None, []] and e is not False for e in [5, "ce", 0, (32, "a")])
True
>>> all(e not in [None, []] and e is not False for e in [5, "ce", 0, 0, (32, "a")])
False

I noticed that there was a problem using e not in [None, [], False] because 0 in [None, [], False] was giving True.

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.