The and-or trick in python

| categories: logic, programming | tags:

The boolean logic commands and and or have return values in python. Let us first review briefly what these operators do by examples. The typical usage is in conditional statements. First, we look at what kind of values evaluate to "True" or "False" in python. Anything that is "empty" usually evaluates to False, along with the integer 0 and the boolean value of False.

for value in ('', 0, None, [], (), {}, False):
    if value:
        print value, "True"
    else:
        print value, "False"
 False
0 False
None False
[] False
() False
{} False
False False

Objects that are not empty evaluate to "True", along with numbers not equal to 0, and the boolean value True.

for value in (' ', 2, 1, "a", [1], (3,), True):
    if value:
        print value, "True"
    else:
        print value, "False"
  True
2 True
1 True
a True
[1] True
(3,) True
True True

The and and or operators compare two objects. and evaluates to "True" if both objects evaluate to "True" and or evaluates to "True" if either object evaluates to "True". Here are some examples.

a = None
b = 5

if a and b:
    print True
else:
    print False
False
a = None
b = 5

if a or b:
    print True
else:
    print False
True

Now the interesting part. The and and or operators actually return values! With the and operator, each argument is evaluated, and if they all evaluate to True, the last argument is returned. Otherwise the first False argument is returned.

a = 1
b = 5
print a and b
print b and a
print a and False
print a and True
print a and None
print False and a
print None and a
print True and 'a' and 0 and True # first False item is zero
5
1
False
True
None
False
None
0

The or operator returns the first True value or the last value if nothing is True. Note that if a True value is found, the values in the expressions after that value are not evaluated.

print 2 or False
print 0 or False
print 0 or False or 4 or {}
2
False
4

One way you might see this is to set variables depending on what command-line arguments were used in a script. For example:

import sys

# replace this:
if 'plot' in sys.argv:
    PLOT = True
else:
    PLOT = False

# with this
PLOT = 'plot' in sys.argv or False

# later in your code:
if PLOT: 
    # do something to make a plot

Now we get to the and-or trick. The trick is to assign a variable one value if some boolean value is True, and another value if the expression is False.

a = True
b = True

if a and b:
    c = "value1"
else: 
    c = "value2"

print c
value1

We can replace the if/else code above with this one line expression:

a = True
b = True

c = (a and b) and "value1" or "value2"
print c
value1

There is a problem. If the first value evaluates to False, you will not get what you expect:

a = True
b = True

c = (a and b) and None or "value2"
print c
value2

In this case, (a and b) evaluates to True, so we would expect the value of c to be the first value. However, None evaluates to False, so the or operator returns the first "True" value, which is the second value. We have to modify the code so that both the or arguments are True. We do this by putting both arguments inside a list, which will then always evaluate to True. This will assign the first list to c if the expression is True, and the second list if it is False. We wrap the whole thing in parentheses, and then index the returned list to get the contents of the list.

a = True
b = True

c = ((a and b) and [None] or ["value2"])[0]

print c
None
a = True
b = True

c = (not (a and b) and [None] or ["value2"])[0]

print c
value2

This is definitely a trick. I find the syntax difficult to read, especially compared to the more verbose if/else statements. It is interesting though, and there might be places where the return value of the boolean operators might be useful, now that you know you can get them.

Here is a tough example of using this to update a dictionary entry. Previously we used a dictionary to count unique entries in a list.

d = {}

d['key'] = (d.get('key', None) and [d['key'] + 1] or [1])[0]

print d

d['key'] = (d.get('key', None) and [d['key'] + 1] or [1])[0]
print d
{'key': 1}
{'key': 2}

This works because the .get function on a dictionary returns None if the key does not exist, resulting in assigning the value of 1 to that key, or it returns something that evaluates to True if the key does exist, so the key gets incremented.

Let us see this trick in action. Before we used if/else statements to achieve our goal, checking if the key is in the dictionary and incrementing by one if it is, and if not, setting the key to 1.

L = ['a', 'a', 'b','d', 'e', 'b', 'e', 'a']

# old method
d = {}
for el in L:
    if el in d:
        d[el] += 1
    else:
        d[el] = 1

print d
{'a': 3, 'b': 2, 'e': 2, 'd': 1}

Here is the new method:

# new method:
L = ['a', 'a', 'b','d', 'e', 'b', 'e', 'a']
d = {}
for el in L:
    d[el] = (d.get(el, None) and [d[el] + 1] or [1])[0]
print d
{'a': 3, 'b': 2, 'e': 2, 'd': 1}

We have in (an admittedly hard to read) a single single line replaced the if/else statement! I have for a long time thought this should possible. I am somewhat disappointed that it is not easier to read though.

Update 7/8/2013

1 Using more modern python syntax than the and-or trick

@Mark_ pointed out in a comment the more modern syntax in python is "value1" if a else "value2". Here is how it works.

a = True
c = "value1" if a else "value2"
print c
value1
a = ''
c = "value1" if a else "value2"
print c
value2

This is indeed very clean to read. This leads to a cleaner and easier to read implementation of the counting code.

L = ['a', 'a', 'b','d', 'e', 'b', 'e', 'a']
d = {}
for el in L:
    d[el] = (d[el] + 1) if (el in d) else 1
print d
{'a': 3, 'b': 2, 'e': 2, 'd': 1}

See the next section for an even cleaner implementation.

2 using defaultdict

@Mark_ also suggested the use of defaultdict for the counting code. That is pretty concise! It is not obvious that the default value is equal to zero, but int() returns zero. This is much better than the and-or trick.

from collections import defaultdict
print int()

L = ['a', 'a', 'b','d', 'e', 'b', 'e', 'a']
d = defaultdict(int)
for el in L:
    d[el] += 1
print d
0
defaultdict(<type 'int'>, {'a': 3, 'b': 2, 'e': 2, 'd': 1})

Copyright (C) 2013 by John Kitchin. See the License for information about copying.

org-mode source

Discuss on Twitter