Wolfram Research

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"] is performed from choosing one term from each list alternately; however, it is possible to specify a pattern, for example {1,2,1,1,2}, as a third argument.

Examples

Basic Examples

This is a simple example of how it works:

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

It also works with non-numeric terms:

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

Scope

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

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

An example with a new pattern chosen:

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

Example using a list from a binary number as a pattern:

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

Example of how we can use AlternateElements with a random pattern:

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

Possible Issues

AlternateElements only works with two lists at a time:

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

Neat Examples

Practical use of AlternateElements as an example of how to create hybrid (alternated terms) numbers between two real constants with a certain number of digits using Pi and E:

In[25]:=
xa = Pi;
In[26]:=
ya = E;
In[27]:=
na = 50;
In[28]:=
f0 = IntegerDigits@IntegerPart[N[xa*10^(na - 1), na + 10]];
In[29]:=
g0 = IntegerDigits@IntegerPart[N[ya*10^(na - 1), na + 10]];
In[30]:=
N[FromDigits@ResourceFunction["AlternateElements"][f0, g0]/
  10^(na - 1), na]
Out[30]=
In[31]:=
N[FromDigits@ResourceFunction["AlternateElements"][g0, f0]/
  10^(na - 1), na]
Out[31]=

Similar example with a nondefault pattern:

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

Resource History

License Information