You can use np.equal() function as well.
import numpy as np
a = [1,2,4,8,12,13]
x = np.array(a)
res = np.equal((x+1)[:-1], x[1:])
print(res)
Out:
[ True False False False True]
Note:
If you need speed, as it is usual the case, when we use numpy, it is worth to mention, that this methode is faster then np.roll(), what FBruzzesi proposed below and which is an elegant solution as well anyway:
import timeit
code1 = """
import numpy as np
a = [1,2,4,8,12,13]
x = np.array(a)
np.equal((x+1)[:-1], x[1:])
"""
elapsed_time1 = timeit.timeit(code1, number=10000)/100
print(elapsed_time1)
code2 = """
import numpy as np
a = [1,2,4,8,12,13]
x = np.array(a)
x+1 == np.roll(x, shift=-1)
"""
elapsed_time2 = timeit.timeit(code2, number=10000)/100
print(elapsed_time2)
Out:
0.00044608700000026147
0.0022752689999970244