How to | Create Lists

Lists are very important and general structures in Mathematica. They allow you to treat collections of all kinds of objects as a single entity. There are many ways to construct them.

Use the shorthand notation to make a list:

In[1]:=
Click for copyable input
Out[1]=

Or use List, which automatically is changed to :

In[2]:=
Click for copyable input
Out[2]=

Use Range with one argument to create a list of integers starting at 1:

In[3]:=
Click for copyable input
Out[3]=

Or use Range with two arguments to create a list of integers starting higher:

In[4]:=
Click for copyable input
Out[4]=

With three arguments the offset can be different than 1:

In[5]:=
Click for copyable input
Out[5]=

This squares each element of the list:

In[6]:=
Click for copyable input
Out[6]=

Or use Table to create the first 10 squares:

In[7]:=
Click for copyable input
Out[7]=

Just like Range, Table can start higher or jump by any amount:

In[8]:=
Click for copyable input
Out[8]=

Use NestList to create a list of the results of applying to for 0 through 3 times:

In[9]:=
Click for copyable input
Out[9]=

Use Array to create a list of length 4, with elements :

In[10]:=
Click for copyable input
Out[10]=

This gives a 3×2 array:

In[11]:=
Click for copyable input
Out[11]=

Use List to create lists of strings:

In[12]:=
Click for copyable input
Out[12]=
    

A matrix in Mathematica is a list of lists.

Use RandomInteger to create a 4×4 matrix of random integers between 0 and 10 (stored as ):

In[13]:=
Click for copyable input
Out[13]=

Use MatrixForm to see as a 2D matrix:

In[14]:=
Click for copyable input
Out[14]//MatrixForm=
    

You can apply functions to a list.

You can directly apply math functions to a list:

In[15]:=
Click for copyable input
Out[15]=

Math functions keep going deeper:

In[16]:=
Click for copyable input
Out[16]=

Some functions give a number as a result:

In[17]:=
Click for copyable input
Out[17]=

Length gives the length of a list:

In[18]:=
Click for copyable input
Out[18]=

Use Map to apply a function to the elements of a list (not needed for math functions):

In[19]:=
Click for copyable input
Out[19]=

This uses Map to apply Length to each sublist:

In[20]:=
Click for copyable input
Out[20]=

Similarly, this finds the maximum of each sublist:

In[21]:=
Click for copyable input
Out[21]=
New to Mathematica? Find your learning path »
Have a question? Ask support »