Benchmarks: test1 = [False] * 100 + [True] * 2 test2 = [True] * 100 + [False] * 2
TIMER.repeat([ lambda: xor(test1), # 0.0168 lambda: xor(test2), # 0.0172 lambda: xor_ss(test1), # 0.1392 lambda: xor_ss(test2), # 0.0084 lambda: xor_new(test1), # 0.0116 lambda: xor_new(test2), # 0.0074 lambda: all(test1), # 0.0016 lambda: all(test2) # 0.0046 ]) Your first function is fairly slow. Second one deals with short-circuiting, but is super slow on full search. `xor_new` is the best what I could achieve using python builtins. But builtin `all` has the best performance. DG > On 13 Nov 2023, at 23:20, Michael Speer <knome...@gmail.com> wrote: > > I don't think an exclusive-or/truthy-entries-count-checker needs to be a > builtin by any stretch. > > >>> def xor( iterable, n = 1 ): > ... return sum( map( bool, iterable ) ) == n > > Or if you insist on short circuiting: > > >>> def xor_ss( iterable, n = 1 ): > ... for intermediate in itertools.accumulate( iterable, (lambda x, y: x + > bool(y)), initial = 0 ): > ... if intermediate > n: > ... return False > ... return intermediate == n > > > > On Mon, Nov 13, 2023 at 4:05 PM Barry via Python-list <python-list@python.org > <mailto:python-list@python.org>> wrote: > > > > On 13 Nov 2023, at 17:48, Dom Grigonis <dom.grigo...@gmail.com > > <mailto:dom.grigo...@gmail.com>> wrote: > > > > Short circuiting happens, when: > > xor([True, True, False, False], n=1) > > At index 1 it is clear that the answer is false. > > Can you share an example with 4 values that is true? > And explain why it is xor. > > Barry > > -- > https://mail.python.org/mailman/listinfo/python-list > <https://mail.python.org/mailman/listinfo/python-list> -- https://mail.python.org/mailman/listinfo/python-list