In Python 3, reduce()
has been moved to functools.reduce()
and apparently it's better to use list comprehensions or plain loops for better readability.
I want to print the XOR'ed value of all elements in a list.
# My implementation with functools
from functools import reduce
print(reduce(lambda a, b: a^b, [1, 3, 2, 3, 4, 4, 5, 2, 1]))
And I have this:
# My implementation without functools
def XOR(x):
ans = 0
for i in x:
ans = ans ^ i
return ans
print(XOR([1, 3, 2, 3, 4, 4, 5, 2, 1]))
How can write a more functional version of this code without reduce()
?
(Please provide references or code in Python 3, if any.)