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.
6 Answers 6
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.
-
Thanks for explaining this, makes sense. Is there a better way to check if the two are not-empty than by checking their lengths?drglove– drglove2015年05月27日 16:29:53 +00:00Commented May 27, 2015 at 16:29
-
2Yes.
if a and b
as you originally had it.kindall– kindall2015年05月27日 16:32:54 +00:00Commented May 27, 2015 at 16:32
a and b
is correct.
and
returns the second argument if the first is true.
-
the second comment, while true is only meaningful outside of an if (like an assignment)Wyrmwood– Wyrmwood2015年05月27日 16:26:41 +00:00Commented May 27, 2015 at 16:26
You can do this
if len(a) and len(b):
#do something
-
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.drglove– drglove2015年05月27日 16:22:41 +00:00Commented May 27, 2015 at 16:22 -
Can you give an example where this would have different behavior from
if a and b:
?Kevin– Kevin2015年05月27日 16:25:26 +00:00Commented May 27, 2015 at 16:25 -
@Kevin, should there be such a situation?ForceBru– ForceBru2015年05月27日 16:28:34 +00:00Commented May 27, 2015 at 16:28
-
1@PeterDeGlopper this works because
if a and b
works. This is exactly the same asif a and b
, only longer.vaultah– vaultah2015年05月27日 16:28:39 +00:00Commented May 27, 2015 at 16:28 -
@ForceBru, if there's no such situation, then why use it over
if a and b:
?Kevin– Kevin2015年05月27日 16:31:22 +00:00Commented May 27, 2015 at 16:31
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])
>>>
and
operator is compare two boolean values
bool([]) == False
so [] and b
always return []
; no matter what b
is.
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.
if a and b:
will only be True if both are not emptyif 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.