Function Repository Resource:

SafeTake

Source Notebook

Safely take elements from lists

Contributed by: Michael Sollami

ResourceFunction["SafeTake"][list,n]

gives the first n of list (as if using UpTo).

ResourceFunction["SafeTake"][list,-n]

safely take the last n of list.

ResourceFunction["SafeTake"][list,{n,m}]

safely take elements n though m of list.

Details and Options

ResourceFunction["SafeTake"] does not issue a message if n{1,,Length@list}.

Examples

Basic Examples (3) 

Give all the elements, up to 10:

In[1]:=
ResourceFunction["SafeTake"][{1, 2, 3}, 10]
Out[1]=

Give the elements from position 3 to 7:

In[2]:=
{3, 4, 5} === ResourceFunction["SafeTake"][Range[5], {3, 7}] === ResourceFunction["SafeTake"][Range[5], 3 ;; 7]
Out[2]=

Take the last 100 elements:

In[3]:=
ResourceFunction["SafeTake"][Range[9], -100]
Out[3]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 06 April 2020

Related Resources

License Information