I think these 3 are logically equivalent, returning the set {1, 3, 4} :
set(sum(((1, 3), (4,), (1,)), ())) set(sum([[1, 3], [4], [1]], [])) functools.reduce(operator.or_, ({1, 3}, {4}, {1}), set())
But when I try to test everyone's performance in ipython (v1.2.1 on python 3.4.0), the timeit magic stops working.
In [1]: from operator import or_; from functools import reduce In [2]: timeit set(sum([[1, 3], [4], [1]], [])) 1000000 loops, best of 3: 604 ns per loop In [3]: timeit set(sum(((1, 3), (4,), (1,)), ())) 1000000 loops, best of 3: 330 ns per loop In [4]: timeit reduce(or_, ({1, 3}, {4}, {1}), set()) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-83628f6293f3> in <module>() ----> 1 get_ipython().magic('timeit reduce(or_, ({1, 3}, {4}, {1}), set())') /usr/lib/python3/dist-packages/IPython/core/interactiveshell.py in magic(self, arg_s) 2164 magic_name, _, magic_arg_s = arg_s.partition(' ') 2165 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) -> 2166 return self.run_line_magic(magic_name, magic_arg_s) 2167 2168
What's going on here? Also fails in 2.7. I cannot reproduce this using the vanilla python timeit.timeit .