Your a is what combinators call a multiset. The sympy library has various routines for working with them.
>>> from sympy.utilities.iterables import multiset_permutations >>> import numpy as np >>> a = np.array([0, 1, 0, 2]) >>> for p in multiset_permutations(a): ... p ... [0, 0, 1, 2] [0, 0, 2, 1] [0, 1, 0, 2] [0, 1, 2, 0] [0, 2, 0, 1] [0, 2, 1, 0] [1, 0, 0, 2] [1, 0, 2, 0] [1, 2, 0, 0] [2, 0, 0, 1] [2, 0, 1, 0] [2, 1, 0, 0]