157

What is the most efficient way to toggle between 0 and 1?

  • while this question asks how to toggle values in binary way most efficiently, some answers explain cycling through (arbitrary) values, e.g. https://stackoverflow.com/a/61041907/537865 – mad Apr 05 '20 at 11:37

16 Answers16

336

Solution using NOT

If the values are boolean, the fastest approach is to use the not operator:

>>> x = True
>>> x = not x        # toggle
>>> x
False
>>> x = not x        # toggle
>>> x
True
>>> x = not x        # toggle
>>> x
False

Solution using subtraction

If the values are numerical, then subtraction from the total is a simple and fast way to toggle values:

>>> A = 5
>>> B = 3
>>> total = A + B
>>> x = A
>>> x = total - x    # toggle
>>> x
3
>>> x = total - x    # toggle
>>> x
5
>>> x = total - x    # toggle
>>> x
3

Solution using XOR

If the value toggles between 0 and 1, you can use a bitwise exclusive-or:

>>> x = 1
>>> x ^= 1
>>> x
0
>>> x ^= 1
>>> x
1

The technique generalizes to any pair of integers. The xor-by-one step is replaced with a xor-by-precomputed-constant:

>>> A = 205
>>> B = -117
>>> t = A ^ B        # precomputed toggle constant
>>> x = A
>>> x ^= t           # toggle
>>> x
-117
>>> x ^= t           # toggle
>>> x
205
>>> x ^= t           # toggle
>>> x
-117

(This idea was submitted by Nick Coghlan and later generalized by @zxxc.)

Solution using a dictionary

If the values are hashable, you can use a dictionary:

>>> A = 'xyz'
>>> B = 'pdq'
>>> d = {A:B, B:A}
>>> x = A
>>> x = d[x]         # toggle
>>> x
'pdq'
>>> x = d[x]         # toggle
>>> x
'xyz'
>>> x = d[x]         # toggle
>>> x
'pdq'

Solution using a conditional expression

The slowest way is to use a conditional expression:

>>> A = [1,2,3]
>>> B = [4,5,6]
>>> x = A
>>> x = B if x == A else A
>>> x
[4, 5, 6]
>>> x = B if x == A else A
>>> x
[1, 2, 3]
>>> x = B if x == A else A
>>> x
[4, 5, 6]

Solution using itertools

If you have more than two values, the itertools.cycle() function provides a generic fast way to toggle between successive values:

>>> import itertools
>>> toggle = itertools.cycle(['red', 'green', 'blue']).__next__
>>> toggle()
'red'
>>> toggle()
'green'
>>> toggle()
'blue'
>>> toggle()
'red'
>>> toggle()
'green'
>>> toggle()
'blue'
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • The last example seems so slick and intuitive, but doesn't work in Python 3+ with the removal of .next(). Is there a way to make it work similarly in later version of python? – labarna Jul 18 '17 at 04:24
  • 4
    @labarna In Python 3, the `.next()` has been replaced by a global `next()` function. The above example would be: `toggle = itertools.cycle(...); next(toggle)` – elpres Jul 18 '17 at 04:50
  • 2
    `toggle = itertools.cycle(['red', 'green', 'blue'])` `next(toggle)` – Maximilian Jul 18 '17 at 04:50
  • 8
    The XOR example can be generalised to toggle between values `a` and `b` using `x = x ^ (a ^ b)`. – zxxc Jul 18 '17 at 08:36
  • `int(not 0)` and `int(not 1)`... hrmmm – jhrr Jan 31 '19 at 21:39
  • 2
    Instead of calling it `toggle`, call it `color` so that you can say `next(color)` to get the... next color of course :) – Asocia Sep 09 '20 at 16:21
41

I always use:

p^=True

If p is a boolean, this switches between true and false.

renger
  • 775
  • 1
  • 7
  • 11
  • 1
    Perfect! `p` doesn't need to be referenced twice for this method to work!! Idea if you are toggling a value with a long long reference. – ThorSummoner Dec 12 '14 at 07:16
  • 1
    what is this operator called? – mix3d Sep 20 '17 at 12:11
  • 6
    This is the XOR operator. – bastelflp Nov 14 '17 at 23:44
  • 2
    @mix3d Precisely it is "bitwise exclusive or" (as opposed to "logical exclusive or") - https://wiki.python.org/moin/BitwiseOperators. Logical XOR [doesn't have](https://stackoverflow.com/q/432842/149428) a specific operator in Python in general but you can find it implemented [in some special cases](https://docs.python.org/3/search.html?q=logical_xor) like in the decimal module. – Taylor D. Edmiston Jan 31 '19 at 22:08
  • @mix3d `^=` is bitwise xor assigment – wjandrea Jul 06 '19 at 03:44
  • The one problem with using the XOR operator is that since it's bitwise, it applies the calculation to the last bit, meaning that if `p` suddenly became an integer it will apply the operation only to the last bit, as a pose to the value itself. – MaximV Oct 06 '20 at 20:33
27

Here is another non intuitive way. The beauty is you can cycle over multiple values and not just two [0,1]

For Two values (toggling)

>>> x=[1,0]
>>> toggle=x[toggle]

For Multiple Values (say 4)

>>> x=[1,2,3,0]
>>> toggle=x[toggle]

I didn't expect this solution to be almost the fastest too

>>> stmt1="""
toggle=0
for i in xrange(0,100):
    toggle = 1 if toggle == 0 else 0
"""
>>> stmt2="""
x=[1,0]
toggle=0
for i in xrange(0,100):
    toggle=x[toggle]
"""
>>> t1=timeit.Timer(stmt=stmt1)
>>> t2=timeit.Timer(stmt=stmt2)
>>> print "%.2f usec/pass" % (1000000 * t1.timeit(number=100000)/100000)
7.07 usec/pass
>>> print "%.2f usec/pass" % (1000000 * t2.timeit(number=100000)/100000)
6.19 usec/pass
stmt3="""
toggle = False
for i in xrange(0,100):
    toggle = (not toggle) & 1
"""
>>> t3=timeit.Timer(stmt=stmt3)
>>> print "%.2f usec/pass" % (1000000 * t3.timeit(number=100000)/100000)
9.84 usec/pass
>>> stmt4="""
x=0
for i in xrange(0,100):
    x=x-1
"""
>>> t4=timeit.Timer(stmt=stmt4)
>>> print "%.2f usec/pass" % (1000000 * t4.timeit(number=100000)/100000)
6.32 usec/pass
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • 1
    yeah thats schweet as a nut. thanks everyone this is fun looking at how different people approach the problem (and informative.) –  Dec 05 '11 at 06:51
  • Nice, it's a miniature state machine. – kindall Dec 05 '11 at 07:30
  • well, your one is the most interesting, but its not what i personally need for the thing i was asking about, so ok, i think the simple math one then is probably the best for me, shouldnt that be 1-x there ? –  Dec 05 '11 at 15:02
  • Yep, but that shouldn't make the speed any different. – Blender Dec 05 '11 at 17:02
  • ai, but it would make it wrong though wouldnt it ? some great answers here, SO rocks! –  Dec 05 '11 at 20:02
  • @Blender this toggle has virtually nothing to do with Python. It can be implemented with any structure that can support the same type for keys and values. Like kindall said, a miniature state machine. – Arnaud P Dec 13 '13 at 18:14
19

The not operator negates your variable (converting it into a boolean if it isn't already one). You can probably use 1 and 0 interchangeably with True and False, so just negate it:

toggle = not toggle

But if you are using two arbitrary values, use an inline if:

toggle = 'a' if toggle == 'b' else 'b'
Blender
  • 289,723
  • 53
  • 439
  • 496
  • 1
    +1 but `toggle = 0 if toggle else 1` is shorter and more general – luc Dec 05 '11 at 06:48
  • Sorry, I'll swap variables to make it clearer. I was using the inline `if` to toggle between two *arbitrary* variables, not just `1` and `0`. – Blender Dec 05 '11 at 06:50
16

Just between 1 and 0, do this

1-x 

x can take 1 or 0

M S
  • 3,995
  • 3
  • 25
  • 36
  • Since (in Python 2.x, anyway) `True` and `False` are actually integers, albeit ones with a surprisingly verbose `__str__()` method, `x` can also be `True` or `False` here. You'll get 1 or 0 back, though. – kindall Dec 05 '11 at 07:32
13

Trigonometric approach, just because sin and cos functions are cool.

enter image description here

>>> import math
>>> def generator01():
...     n=0
...     while True:
...         yield abs( int( math.cos( n * 0.5 * math.pi  ) ) )
...         n+=1
... 
>>> g=generator01() 
>>> g.next()
1
>>> g.next()
0
>>> g.next()
1
>>> g.next()
0
dani herrera
  • 48,760
  • 8
  • 117
  • 177
8

Surprisingly nobody mention good old division modulo 2:

In : x = (x + 1)  % 2 ; x
Out: 1

In : x = (x + 1)  % 2 ; x
Out: 0

In : x = (x + 1)  % 2 ; x
Out: 1

In : x = (x + 1)  % 2 ; x
Out: 0

Note that it is equivalent to x = x - 1, but the advantage of modulo technique is that the size of the group or length of the interval can be bigger then just 2 elements, thus giving you a similar to round-robin interleaving scheme to loop over.

Now just for 2, toggling can be a bit shorter (using bit-wise operator):

x = x ^ 1
Yauhen Yakimovich
  • 13,635
  • 8
  • 60
  • 67
  • I am not sure how "pythonic" is this (C-like) modulo arithmetic (i,e, whether "pythonic" applies?). I guess it's just arithmetic, works everywhere else where you have binary. – Yauhen Yakimovich Jun 19 '13 at 12:02
  • Obviously the finite-state-machine with tuple like x=(1,2,3,0); token=0;token=x[token] is extremely exciting, since it can be even more general than just group operation. – Yauhen Yakimovich Jun 19 '13 at 12:08
7

one way to toggle is by using Multiple assignment

>>> a = 5
>>> b = 3

>>> t = a, b = b, a
>>> t[0]
3

>>> t = a, b = b, a
>>> t[0]
5

Using itertools:

In [12]: foo = itertools.cycle([1, 2, 3])

In [13]: next(foo)
Out[13]: 1

In [14]: next(foo)
Out[14]: 2

In [15]: next(foo)
Out[15]: 3

In [16]: next(foo)
Out[16]: 1

In [17]: next(foo)
Out[17]: 2
hugo24
  • 1,089
  • 13
  • 21
6

The easiest way to toggle between 1 and 0 is to subtract from 1.

def toggle(value):
    return 1 - value
Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106
5

Using exception handler

>>> def toogle(x):
...     try:
...         return x/x-x/x
...     except  ZeroDivisionError:
...         return 1
... 
>>> x=0
>>> x=toogle(x)
>>> x
1
>>> x=toogle(x)
>>> x
0
>>> x=toogle(x)
>>> x
1
>>> x=toogle(x)
>>> x
0

Ok, I'm the worst:

enter image description here

import math
import sys

d={1:0,0:1}
l=[1,0]

def exception_approach(x):
    try:
        return x/x-x/x
    except  ZeroDivisionError:
        return 1

def cosinus_approach(x):
    return abs( int( math.cos( x * 0.5 * math.pi  ) ) )

def module_approach(x):
    return  (x + 1)  % 2

def subs_approach(x):
    return  x - 1

def if_approach(x):
    return 0 if x == 1 else 1

def list_approach(x):
    global l
    return l[x]

def dict_approach(x):
    global d
    return d[x]

def xor_approach(x):
    return x^1

def not_approach(x):
    b=bool(x)
    p=not b
    return int(p)

funcs=[ exception_approach, cosinus_approach, dict_approach, module_approach, subs_approach, if_approach, list_approach, xor_approach, not_approach ]

f=funcs[int(sys.argv[1])]
print "\n\n\n", f.func_name
x=0
for _ in range(0,100000000):
    x=f(x)
Ikem Krueger
  • 186
  • 1
  • 1
  • 12
dani herrera
  • 48,760
  • 8
  • 117
  • 177
3

How about an imaginary toggle that stores not only the current toggle, but a couple other values associated with it?

toggle = complex.conjugate

Store any + or - value on the left, and any unsigned value on the right:

>>> x = 2 - 3j
>>> toggle(x)
(2+3j)

Zero works, too:

>>> y = -2 - 0j
>>> toggle(y)
(-2+0j)

Easily retrieve the current toggle value (True and False represent + and -), LHS (real) value, or RHS (imaginary) value:

>>> import math
>>> curr = lambda i: math.atan2(i.imag, -abs(i.imag)) > 0
>>> lhs = lambda i: i.real
>>> rhs = lambda i: abs(i.imag)
>>> x = toggle(x)
>>> curr(x)
True
>>> lhs(x)
2.0
>>> rhs(x)
3.0

Easily swap LHS and RHS (but note that the sign of the both values must not be important):

>>> swap = lambda i: i/-1j
>>> swap(2+0j)
2j
>>> swap(3+2j)
(2+3j)

Easily swap LHS and RHS and also toggle at the same time:

>>> swaggle = lambda i: i/1j
>>> swaggle(2+0j)
-2j
>>> swaggle(3+2j)
(2-3j)

Guards against errors:

>>> toggle(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'conjugate' requires a 'complex' object but received a 'int'

Perform changes to LHS and RHS:

>>> x += 1+2j
>>> x
(3+5j)

...but be careful manipulating the RHS:

>>> z = 1-1j
>>> z += 2j
>>> z
(1+1j) # whoops! toggled it!
Rick
  • 43,029
  • 15
  • 76
  • 119
3

Variables a and b can be ANY two values, like 0 and 1, or 117 and 711, or "heads" and "tails". No math is used, just a quick swap of the values each time a toggle is desired.

a = True   
b = False   

a,b = b,a   # a is now False
a,b = b,a   # a is now True
1

I use abs function, very useful on loops

x = 1
for y in range(0, 3):
    x = abs(x - 1)

x will be 0.

Proteo5
  • 65
  • 4
0

Let's do some frame hacking. Toggle a variable by name. Note: This may not work with every Python runtime.

Say you have a variable "x"

>>> import inspect
>>> def toggle(var_name):
>>>     frame = inspect.currentframe().f_back
>>>     vars = frame.f_locals
>>>     vars[var_name] = 0 if vars[var_name] == 1 else 1

>>> x = 0
>>> toggle('x')
>>> x
1
>>> toggle('x')
>>> x
0
0

If you are dealing with an integer variable, you can increment 1 and limit your set to 0 and 1 (mod)

X = 0  # or X = 1
X = (X + 1)%2
0

Switching between -1 and +1 can be obtained by inline multiplication; used for calculation of pi the 'Leibniz' way (or similar):

sign = 1
result = 0
for i in range(100000):
    result += 1 / (2*i + 1) * sign
    sign *= -1
print("pi (estimate): ", result*4)