7

In Python, I know the pythonic way to check if a list is empty is

if not a:
 # do things with empty list

To check if a list is not empty then, we would do:

if a:
 # do things with my list

How would we check, simultaneously (as read), if two lists then are not empty?

if a and b:
 # do things with my two lists

The above does not seem to work, and I'm unsure what (a and b) actually means. For a = [2], b = [1,3], (a and b) = [1,3]. What is the and operator actually doing here? If I end up reducing b = [] at some point, (a and b) = [] even though a is not empty.

Edit: My use case goes something like

while (a and b are not empty):
 modify a
 modify b

I would have naively thought that since if a checks if a list is not empty, if a and b would check if neither were empty, which is not the case.

asked May 27, 2015 at 16:18
6
  • 2
    possible duplicate of Confusion found with and operator Commented May 27, 2015 at 16:22
  • what do you mean when you say "does not .. work"? Commented May 27, 2015 at 16:23
  • I am having trouble understanding,if a and b: will only be True if both are not empty Commented May 27, 2015 at 16:23
  • I'll edit the question to be more concise and check out the duplicate. This one is hard to search for as 'and' is typically not indexed. Commented May 27, 2015 at 16:24
  • if a and b does check whether or not both lists are empty. If your seeing things that seem otherwise, your problem is in your loop and not the conditional. Commented May 27, 2015 at 16:34

6 Answers 6

9

It's working fine. For a = [2] and b = [1, 3], a and b is returning [1, 3] which is truthy, exactly as you would expect, because True and True is True. When you change b to [] it returns [], which is falsy, again exactly as you would expect, because True and False is False. So if a and b does exactly what you want.

What is actually happening is that and is returning the value that decided the truth of the expression. and doesn't always evaluate both sub-expressions; when the first is falsy, the whole expression is falsy and the second doesn't need to be evaluated, and therefore isn't. This is called short-circuiting. or behaves similarly (skipping evaluating the second part if the first part is truthy). Wherever and or or is able to make its decision, it returns that value.

Another way of looking at it: bool(a) and bool(a) == bool(a and b) in Python.

answered May 27, 2015 at 16:26
2
  • Thanks for explaining this, makes sense. Is there a better way to check if the two are not-empty than by checking their lengths? Commented May 27, 2015 at 16:29
  • 2
    Yes. if a and b as you originally had it. Commented May 27, 2015 at 16:32
2

a and b is correct.

and returns the second argument if the first is true.

answered May 27, 2015 at 16:22
1
  • the second comment, while true is only meaningful outside of an if (like an assignment) Commented May 27, 2015 at 16:26
1

You can do this

if len(a) and len(b):
 #do something
answered May 27, 2015 at 16:20
6
  • I've thought about this, but am wondering if there's something closer to a and b. If not, I'll settle for it, but I'd still like to know what the operator is actually doing here. Commented May 27, 2015 at 16:22
  • Can you give an example where this would have different behavior from if a and b:? Commented May 27, 2015 at 16:25
  • @Kevin, should there be such a situation? Commented May 27, 2015 at 16:28
  • 1
    @PeterDeGlopper this works because if a and b works. This is exactly the same as if a and b, only longer. Commented May 27, 2015 at 16:28
  • @ForceBru, if there's no such situation, then why use it over if a and b:? Commented May 27, 2015 at 16:31
1

I think what you want is

>>> a, b = list(), list()
>>> while not (a and b):
... a.append(1)
... b.append(2)
...
>>> a, b
([1], [2])
>>>
answered May 27, 2015 at 16:32
0

and operator is compare two boolean values

 bool([]) == False

so [] and b always return []; no matter what b is.

answered May 27, 2015 at 16:43
0

To build on kindall's answer - you could also concatenate the lists before doing the truthiness test:

a = []
b = []
c = [1]
bool(a + b) # False
bool(a + b + c) # True

Of course this would come with a performance penalty if you were doing something where that mattered.

answered Jan 2, 2020 at 10:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.