[Which is actually partially incorrect (see PS):]
The following method of obtaining unique elements in all subarrays is very fast:
seq = itertools.chain(tab[indices1].flat, tab[indices2].flat, tab[indices3].flat, tab[indices4].flat) result = set(seq)
Note that flat (which returns an iterator) is used instead of flatten() (which returns a full array) and that set() can be called directly (instead of using map() and a dictionary, as in your second method).
Here are the synchronization results (obtained in the IPython shell):
>>> %timeit result = numpy.unique(numpy.array([tab[indices1], tab[indices2], tab[indices3], tab[indices4]])) 100 loops, best of 3: 8.04 ms per loop >>> seq = itertools.chain(tab[indices1].flat, tab[indices2].flat, tab[indices3].flat, tab[indices4].flat) >>> %timeit set(seq) 1000000 loops, best of 3: 223 ns per loop
In this example, the set / flat method is therefore 40 times faster.
PS : set(seq) is actually not representative. In fact, the first timer loop empties the iterator seq , and subsequent evaluations of set() return an empty set! The right time test is next
>>> %timeit set(itertools.chain(tab[indices1].flat, tab[indices2].flat, tab[indices3].flat, tab[indices4].flat)) 100 loops, best of 3: 9.12 ms per loop
which shows that the set / flat method is actually not faster.
PPS : here is a (unsuccessful) study of the mtrw sentence; looking for unique indexes in advance may be a good idea, but I cannot find a way to implement it faster than the above approach:
>>> %timeit set(indices1).union(indices2).union(indices3).union(indices4) 100 loops, best of 3: 11.9 ms per loop >>> %timeit set(itertools.chain(indices1.flat, indices2.flat, indices3.flat, indices4.flat)) 100 loops, best of 3: 10.8 ms per loop
Thus, the search for the set of all individual indices is rather slow in itself.
numpy.unique(<concatenated array of indices>) : numpy.unique(<concatenated array of indices>) is actually 2-3 times faster than set(<concatenated array of indices>) . This is the key to the acceleration obtained in Bago's answer ( unique(concatenate((…))) ). Probably the reason is that letting NumPy process its arrays by itself is generally faster than pairing pure Python ( set ) with NumPy arrays.
Conclusion : this answer is, therefore, only documents with attempt errors that should not be fully respected, and also, perhaps, a useful note about temporary code with iterators ...