Function Repository Resource:

PreviousNextElement

Source Notebook

Get the elements that appear before and after a given value in a list

Contributed by: Andy Hunt

ResourceFunction["PreviousNextElement"][element,{e1,e2,}]

returns the elements that appear before and after element in {e1,e2,}.

Details and Options

If the element is not found, a list containing Missing["NotFound"] is returned.
If the element is at the beginning or end of the list, None is returned as the corresponding previous or next element.
If element occurs more than one in {e1,e2,}, the first instance is used.
By default, searches will not wrap around the list. With the setting WrapAround True, searches will wrap around the list.

Examples

Basic Examples (2) 

Find previous/next elements in a list of integers:

In[1]:=
ResourceFunction["PreviousNextElement"][3, {1, 2, 3, 4}]
Out[1]=

Find previous/next elements in a list of strings:

In[2]:=
ResourceFunction[
 "PreviousNextElement"]["Dogs", {"Birds", "Cats", "Dogs", "Fish"}]
Out[2]=

Scope (2) 

Elements do not have to be numbers or strings:

In[3]:=
ResourceFunction["PreviousNextElement"][
 "George" -> 3, {"John" -> 1, "Paul" -> 2, "George" -> 3, "Ringo" -> 4}]
Out[3]=

If the element doesn't exist, Missing["NotFound"] is returned:

In[4]:=
ResourceFunction["PreviousNextElement"][1963, {1, 2, 3, 4}]
Out[4]=

Options (2) 

By default, searches will not wrap around the list:

In[5]:=
ResourceFunction["PreviousNextElement"][4, {1, 2, 3, 4}]
Out[5]=

With the option setting WrapAroundTrue, searches will wrap around the list:

In[6]:=
ResourceFunction["PreviousNextElement"][4, {1, 2, 3, 4}, WrapAround -> True]
Out[6]=

Possible Issues (2) 

If elements within the list are not unique, the first match is returned:

In[7]:=
ResourceFunction[
 "PreviousNextElement"][0, {1, 0, 1, 0, 0, 1, 0, 0, 0, 1}]
Out[7]=
In[8]:=
ResourceFunction[
 "PreviousNextElement"][1, {1, 0, 1, 0, 0, 1, 0, 0, 0, 1}]
Out[8]=

Matches are found after evaluation:

In[9]:=
ResourceFunction[
 "PreviousNextElement"][{2 - a + b}, {{b + 3}, {b + 2 - a}, {a + 3*1}}]
Out[9]=

Neat Examples (3) 

Which two U.S. states is Illinois between alphabetically:

In[10]:=
ResourceFunction["PreviousNextElement"][
 Entity["AdministrativeDivision", {"Illinois", "UnitedStates"}], EntityList[
  EntityClass["AdministrativeDivision", "AllUSStatesPlusDC"]]]
Out[10]=

Between which two planets is the Earth when listed in order of increasing radius:

In[11]:=
planetsByRadius = Keys@SortBy[
   PlanetData[PlanetData[], "Radius", "EntityAssociation"], # &]
Out[11]=
In[12]:=
ResourceFunction["PreviousNextElement"][
 Entity["Planet", "Earth"], planetsByRadius]
Out[12]=

Find phrases within text:

In[13]:=
address = ResourceData["Gettysburg Address"];
wordList = Partition[StringSplit[address, WhitespaceCharacter ..], 3];
In[14]:=
ResourceFunction[
 "PreviousNextElement"][{"that", "all", "men"}, wordList]
Out[15]=

Publisher

Andy Hunt

Version History

  • 1.0.0 – 31 May 2023

Related Resources

License Information