2

I am wondering about why [] and {} return [] in python?

>>> [] and {}
[]

Edit: Why are [] and {} false?I understand and or expressions , but i cannot understand [] and {} expressions are false?

3
  • Because they both are False then first is returned. Commented Feb 25, 2018 at 20:43
  • Why [] and {} false?I understand and or expressions , but i cannot understand [] and {} expressions are false? Commented Feb 25, 2018 at 21:02
  • 1
    @mw-b, ...because they're empty. See ie. empty list boolean value, or How do I check if a list is empty?. Commented Feb 25, 2018 at 21:09

1 Answer 1

3

In the short circuit evaluation containing only operator and (could be multiple) and multiple operators, the expression returns the first falsy value, in this case which is [] -- an empty list.

In the case where all the values are truthy, it returns the last value.

# [] and {} both are falsey
In [77]: [] and {}
Out[77]: []

# 3 is truthy, {} is falsey
In [78]: 3 and {}
Out[78]: {}

# all except {} are truthy
In [79]: 3 and 9 and {}
Out[79]: {}

# you get it...
In [80]: 3 and 9 and {} and 10
Out[80]: {}
Sign up to request clarification or add additional context in comments.

3 Comments

In addition to this, the reason that the first falsy value is returned rather than some other one like the last or middle is that of an optimization technique called short-circuit evaluation.
or the second value if neither are falsey
@avigil Just added.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.