Jan Diepenbrock - 1 year ago 106

Python Question

I'm using python (2.7.2) and I need all permutations without repetition from a list. More precisely,

`for i in itertools.permutations([1,2,3]): print i`

correctly gives

`(1, 2, 3)`

(1, 3, 2)

(2, 1, 3)

(2, 3, 1)

(3, 1, 2)

(3, 2, 1)

But now I am looking for something would do the following

`for i in myfunction([1,2,2]): print i`

(1,2,2)

(2,2,1)

(2,1,2)

Whereas itertools would give that list twice (or, for ([1,1,1]) as the input, it is just repeated six times).

This is basically something between itertools.permutations and itertools.combinations.

I tried working with sets, but all solutions that I tried always created new problems and never matched the desired output.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

It looks like you want the set.

```
s = []
for i in itertools.permutations([1,2,2]):
s.append(i)
print(set(s))
```

Which gives:

```
{(1, 2, 2), (2, 2, 1), (2, 1, 2)}
```

Alternatively, without saving the objects to a variable:

```
for i in set(itertools.permutations([1,2,2])):
print(i)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**