Function Repository Resource:

AssociationPartition

Source Notebook

Partition an association into a list of associations

Contributed by: Wolfram Research

ResourceFunction["AssociationPartition"][assoc,n]

partitions the association assoc into a list of nonoverlapping associations of length n.

ResourceFunction["AssociationPartition"][assoc,n,d]

generates associations with offset d.

Details and Options

In ResourceFunction["AssociationPartition"][assoc,args], args follow the same conventions as in Partition[list,args].

Examples

Basic Examples (2) 

Partition into associations of length 2:

In[1]:=
ResourceFunction[
 "AssociationPartition"][<|a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6|>, 2]
Out[1]=

Partition into associations of length 3 with offset 1:

In[2]:=
ResourceFunction[
 "AssociationPartition"][<|a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6|>, 3, 1]
Out[2]=

Scope (2) 

Incomplete sublists at the end are by default dropped:

In[3]:=
ResourceFunction[
 "AssociationPartition"][<|a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6, g -> 7|>, 3]
Out[3]=
In[4]:=
ResourceFunction[
 "AssociationPartition"][<|a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6, g -> 7|>, 3, 1]
Out[4]=

Allow for a "ragged" partition, wherein last elements can be shorter:

In[5]:=
ResourceFunction[
 "AssociationPartition"][<|a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6, g -> 7|>, UpTo[3]]
Out[5]=
In[6]:=
ResourceFunction[
 "AssociationPartition"][<|a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6, g -> 7|>, UpTo[3], 1]
Out[6]=

Properties and Relations (2) 

AssociationPartition extends Partition to work on associations:

In[7]:=
Partition[{a, b, c, d, e}, 2]
Out[7]=
In[8]:=
ResourceFunction[
 "AssociationPartition"][<|a -> 1, b -> 2, c -> 3, d -> 4, e -> 5|>,
  2]
Out[8]=

A partitioned List of associations can be re-combined using Association:

In[9]:=
ResourceFunction[
 "AssociationPartition"][<|a -> 1, b -> 2, c -> 3, d -> 4, e -> 5, f -> 6|>, 2]
Out[9]=
In[10]:=
Association[%]
Out[10]=

Version History

  • 1.0.0 – 29 May 2019

Related Resources

License Information