Function Repository Resource:

AssociationPermutations

Source Notebook

Get permutations of the key-value pairs in an Association

Contributed by: Bob Sandheinrich

ResourceFunction["AssociationPermutations"][assoc]

generates a list of all possible permutations of the key-value elements in the Association assoc.

ResourceFunction["AssociationPermutations"][assoc,len]

gives permutations of the specified lengths.

Details and Options

A permutation of an Association is a permutation of the key-value pairs. The value associated with a key in the output is always the same as in the input.
The length specification len can be any of the following:
npermutations containing at most n key-value pairs
{n}permutations containing exactly n key-value pairs
Allpermutations of all lengths
{min,max}permutations of length between min and max
{min,max,d}lengths from min to max in steps of d
There are n! permutations of a list of n distinct elements.
ResourceFunction["AssociationPermutations"][assoc] is effectively equivalent to ResourceFunction["AssociationPermutations"][assoc,{Length[assoc]}].
ResourceFunction["AssociationPermutations"][assoc,All] is equivalent to ResourceFunction["AssociationPermutations"][assoc,Length[assoc]].

Examples

Basic Examples (2) 

Get all full-length permutations of an Association with three values:

In[1]:=
ResourceFunction[
 "AssociationPermutations"][<|"a" -> 1, "b" -> 2, "c" -> 3|>]
Out[1]=

Get permutations of length two:

In[2]:=
ResourceFunction[
 "AssociationPermutations"][<|"a" -> 1, "b" -> 2, "c" -> 3|>, {2}]
Out[2]=

Scope (3) 

Get permutations of all lengths, shortest ones first:

In[3]:=
ResourceFunction[
 "AssociationPermutations"][<|"a" -> 1, "b" -> 2, "c" -> 3|>, All]
Out[3]=

Lengths from one to two:

In[4]:=
ResourceFunction[
 "AssociationPermutations"][<|"a" -> 1, "b" -> 2, "c" -> 3|>, {1, 2}]
Out[4]=

Lengths from one to three with step size two:

In[5]:=
ResourceFunction[
 "AssociationPermutations"][<|"a" -> 1, "b" -> 2, "c" -> 3|>, {1, 3, 2}]
Out[5]=

Applications (2) 

Kids love Yahtzee, but they always lose the dice. None of these kids have the requisite six dice to play:

In[6]:=
dicecounts = <|"Bob" -> 3, "Laura" -> 2, "Jane" -> 4, "Ed" -> 1, "Thomas" -> 3|>
Out[6]=

Luckily, they can play together. Find the groups of kids that have at least six dice:

In[7]:=
DeleteDuplicates[
 Sort /@ Keys@
   Select[ResourceFunction["AssociationPermutations"][dicecounts, All], Total[#] >= 6 &]]
Out[7]=

Properties and Relations (2) 

Permutations does not support Association:

In[8]:=
assoc = <|"Elmo" -> "Red", "Big Bird" -> "Yellow", "Cookie Monster" -> "Blue"|>;
Permutations[assoc]
Out[9]=

Use AssociationPermuations instead:

In[10]:=
perms = ResourceFunction["AssociationPermutations"][assoc]
Out[10]=

The result is equivalent to using Permutations on the Keys:

In[11]:=
Keys[perms] === Permutations[Keys[assoc]]
Out[11]=

AssociationPermutations does not evaluate delayed values:

In[12]:=
ResourceFunction[
 "AssociationPermutations"][<|
  "Paper Jam" :> Print["YOU SHALL NOT PRINT"],
  "Sound Dampeners" :> Echo["No Echos"]|>]
Out[12]=

Version History

  • 1.0.0 – 30 September 2020

Related Resources

License Information