Function Repository Resource:

AlternateElements

Source Notebook

Combine specified elements of two lists into a new list

Contributed by: Claudio Lobo Chaib Filho

ResourceFunction["AlternateElements"][list1,list2]

intersperses elements in odd positions from list1 with elements in even positions from list2.

ResourceFunction["AlternateElements"][list1,list2,pattern]

alternates between the lists using the specified pattern (e.g. {2,1,2,2,1}), where the positions of each value in pattern determine where to take from list1 and list2 respectively.

Details and Options

Unlike Riffle, ResourceFunction["AlternateElements"] omits elements from positions in list1 that are included from list2 and vice versa.
By default, ResourceFunction["AlternateElements"] operates by choosing one term from each list in an alternating fashion. However, it is possible to specify a pattern, for example {1,2,1,1,2}, as a third argument.

Examples

Basic Examples (2) 

This is a simple example of how AlternateElements works:

In[1]:=
r = {2, 4, 6, 8, 10, 12, 14};
s = {3, 5, 7, 9, 11, 13, 15};
In[2]:=
ResourceFunction["AlternateElements"][r, s]
Out[2]=

AlternateElements also works with non-numeric terms:

In[3]:=
rs = {a1, "b1", c1, d1, \[HappySmiley], f1, "g1"};
ss = {h2, i2, \[HappySmiley], "k2", l2, m2, n2};
In[4]:=
ResourceFunction["AlternateElements"][rs, ss]
Out[4]=

Scope (4) 

AlternateElements works with lists of different sizes, keeping all elements from the end of the longer list once the shorter list is exhausted:

In[5]:=
t = {2, 4, 6, 8, 10};
u = {3, 5, 7, 9, 11, 13, aa, 17};
In[6]:=
ResourceFunction["AlternateElements"][t, u]
Out[6]=
In[7]:=
ResourceFunction["AlternateElements"][u, t]
Out[7]=

An example with a new pattern chosen:

In[8]:=
l = {2, 4, 6, 8, a0, 12, 14, 16, 18, 20, 22, 24};
m = {3, 5, 7, 9, 11, 13, 15, 17, b0, 21, 23, 25};
In[9]:=
ResourceFunction["AlternateElements"][l, m, {1, 2, 2, 2, 1, 2}]
Out[9]=

Use a list from a binary number as a pattern:

In[10]:=
p = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30};
q = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31};
In[11]:=
pat = 2 - IntegerDigits[854, 2]
Out[11]=
In[12]:=
ResourceFunction["AlternateElements"][p, q, pat]
Out[12]=

AlternateElements can also be used with a random pattern:

In[13]:=
g = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30};
h = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31};
In[14]:=
ResourceFunction["AlternateElements"][g, h, 2 - IntegerDigits[RandomInteger[{4, 1000}], 2]]
Out[14]=

Possible Issues (1) 

AlternateElements only works with two lists at a time:

In[15]:=
rz = {abc, 2, 4, ch, let};
sz = {3, 5, 7, 9, bb, 16, 18};
tz = {11, cc, 13, 14, 15};
In[16]:=
ResourceFunction["AlternateElements"][sz, rz, tz, {2, 1, 1, 1}]
Out[16]=

Neat Examples (2) 

As a practical use of AlternateElements, you can create hybrid numbers of a given length with alternating digits taken from two real constants. This example illustrates that technique using Pi and E:

In[17]:=
xa = Pi;
ya = E;
na = 50;
In[18]:=
f0 = IntegerDigits@IntegerPart[N[xa*10^(na - 1), na + 10]];
g0 = IntegerDigits@IntegerPart[N[ya*10^(na - 1), na + 10]];
In[19]:=
N[FromDigits@ResourceFunction["AlternateElements"][f0, g0]/
  10^(na - 1), na]
Out[19]=
In[20]:=
N[FromDigits@ResourceFunction["AlternateElements"][g0, f0]/
  10^(na - 1), na]
Out[20]=

Here is a similar example with a nondefault pattern:

In[21]:=
h0 = RealDigits[N[xa, na + 1]][[1]];
i0 = RealDigits[N[ya, na + 1]][[1]];
In[22]:=
N[FromDigits@
   ResourceFunction["AlternateElements"][h0, i0, {1, 2, 1, 1, 2, 2}]/
  10^na, na]
Out[22]=
In[23]:=
N[FromDigits@
   ResourceFunction["AlternateElements"][i0, h0, {2, 2, 1, 1, 1, 2}]/
  10^na, na]
Out[23]=

Publisher

Claudio Lobo Chaib Filho

Version History

  • 1.0.0 – 21 June 2019

Author Notes

Only works at two lists at a time ( just as the definition already typifies ). Can work with other characters, but not with strings.

License Information