# 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)])
```

## Answers:

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)]
```