Function Repository Resource:

PrefixQ

Source Notebook

Test if the first elements of a list are the same as those from another list

Contributed by: Taliesin Beynon

ResourceFunction["PrefixQ"][list1,list2]

gives True if list2 is a prefix of list1.

ResourceFunction["PrefixQ"][list2]

is the operator form of ResourceFunction["PrefixQ"].

Examples

Basic Examples (3) 

The second list starts with the same two elements of the first:

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

A list with other elements:

In[2]:=
ResourceFunction["PrefixQ"][{x, y, z, 1, 2, 3}, {x}]
Out[2]=

A non-prefix list:

In[3]:=
ResourceFunction["PrefixQ"][{"r", "s", "t"}, {"x"}]
Out[3]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 04 February 2019

License Information