# How to remove repeated permutations?

I want to get the Permutations on the elements of a list. Then I'm doing this:

a = Drop[Permutations[Range[1, 16], 2], {1, 17}]
i = 15;
While[i <= (240 - 16), a = Drop[a, {i, i}]; i = (i + (16 - 1))]

The first line of the code gives me all the permutations, but I do not want repetitions, like: I already have {1,2}, I do not want the {2,1}.

I'm trying to do this way but there's no result yet.

Any help?

• I am still confused about how what you want differs from Subsets[list, {2}]. Mar 20, 2012 at 17:05
• Possible duplicates: (44), (1302) Jan 29, 2016 at 23:18

Did I understand correctly?

Subsets[Range[1, 16], {2}]

EDIT: If you want to use Permutations, you could use

DeleteDuplicates[Permutations[Range[16], {2}], Sort[#1] == Sort[#2] &]

which deletes all "duplicates", where "duplicate" is defined by the equality of the two lists when sorted (ie, {2,3} is "equal" to {3,2} for purposes of this comparison).

EDIT: The meaning of #1 and #2 may be demonstrated by this example:

f={#1,#2}&

and then f[a,b] evaluates to {a,b}. That is, you are defining a pure function which takes two arguments, returning a list containing the two arguments, and assigning it to f. This could also be useful.

In the DeleteDuplicates example above, I am using as a test function (see second usage example in the documentation and also this example) that considers two lists equal if they are the same after sorting; thus, {3,4} is equal to {4,3}, since when sorted they both become {3,4}.

• What's the meaning of the #'s? I know # is a slot and it have something to do with patterns, but I never undertood how to use them. Any reference? Mar 19, 2012 at 20:12
• does this reference.wolfram.com/mathematica/ref/Slot.html help?
– acl
Mar 19, 2012 at 20:47
• It's the same of the Mathematica help. It's strange because there's a simple description on the slot thing, but it's always used on a bigger scope. Like.... With that information, i couldn't produce something like your code: DeleteDuplicates[Permutations[Range[16], {2}], Sort[#1] == Sort[#2] &] Mar 19, 2012 at 22:29
– acl
Mar 19, 2012 at 22:31

You could try this as an alternative:

Sort /@ Permutations[Range@16, {2}] // Union
• Union considers {1,2} = {2,1}, isn't it? I didn't remember of using basic math tools. Mar 19, 2012 at 20:18
• What's the function of Sort/@? I've looked at this page [link] (reference.wolfram.com/mathematica/ref/…) and i know it orders elements on a list but the elements of my list are already ordered and simply doing Union@a wont work, only Sort /@ a // Union Mar 19, 2012 at 22:35
• Union[{{2, 1}, {1, 2}}] returns {{2,1},{1,2}} so it seems for Union[] order is important. Perhaps not what you would expect. Mar 20, 2012 at 0:28
• The expression /@ maps the function Sort[] over all the values returned by the call to Permutations. The effect is to sort each permutation into numerical order. This makes {2,1} into {1,2} so that it matches {1,2} when Union[] is applied and the duplicates are then removed. Mar 20, 2012 at 0:32
– CHM
Mar 20, 2012 at 4:01

@acl showed the best way. Because your list is defined as a Range, the particular solution for the case you gave is also found by

Table[{j, k}, {j, 15}, {k, j + 1, 16}]~Flatten~1

EDIT: Here's another way to solve it via Permutations:

Select[Permutations[Range@16, {2}], #[[2]] > #[[1]] &]

As TomD noted, this can be expressed alternatively as:

Select[Permutations[Range@16, {2}], OrderedQ]
• Or, as a slight variation: Select[Permutations[Range@16, {2}], OrderedQ] Mar 20, 2012 at 4:53
• @TomD Nice idea: OrderedQ as a selection criterion. Mar 20, 2012 at 7:43

In addition to the other approaches that have already been given, you can use DeleteDuplicatesBy[list, Sort], which is new in 10.