# How to produce permutations with replacement in Python

## Question:

I am trying to write some code (as part of a larger script) to develop numpy arrays of length n, which I can use to change the sign of an input list of length n, in all possible ways.
I am trying to produce all possible permutations of 1 and -1 of length n.

If I use `itertools.permutations`, it will not accept a repeat length greater than 2, because of not allowing repetitions. If I use `itertools.combinations_with_replacement`, then not all of the permutations are produced. I need “permutations_with_replacement”.
I tried to use `itertools.product`, but I cannot get it to work.

Here is my code so far (n is an unknown number, depending on the length of the input list).

``````import numpy as np
import itertools

ones = [-1, 1]
multiplier = np.array([x for x in itertools.combinations_with_replacement(ones, n)])
``````

Perhaps this is what you want?

``````>>> import itertools
>>> choices = [-1, 1]
>>> n = 3
>>> l = [choices] * n
>>> l
[[-1, 1], [-1, 1], [-1, 1]]
>>> list(itertools.product(*l))
[(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)]
``````
Categories: questions
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.