Lists are a core part of the Wolfram Language.
A list is used to store a sequence of expressions.
You create a list by
entering comma-separated elements inside braces.
But lists do not have to contain sequences of numbers.
They can contain sequences of ALL types of expressions.
A common way to create a list is with the Table function.
The first argument is an expression,
and the second argument is a list
containing an index, starting value, and stopping value.
The table function is extremely versatile.
In the second argument,
if you pass in a list with just an index and a number,
the Table function will assume you want to start with 1.
You can also build a table
by providing the starting value, stopping value,
and how much to change the index in each step.
This will iterate from 1 to 20 in steps of 2.
Mathematica and the Wolfram Language have many thousands of built-in functions.
And many of those functions take lists as arguments.
For example,
to graph a function, you use the Plot function.
The first argument is the function,
and the second argument is a list
containing the variable, the starting value, and the stopping value for the graph.
Another function that uses lists is the Sum function.
For example,
if you want to compute the sum of the values of
x^3 - 5 x, as x ranges from -7 to 7,
you use the Sum function..
with the expression as the first argument..
and a list with the index, starting value, and stopping value as the second argument.
This sum is zero.
This isn’t a mistake!
If you look at the Table of values,
you will see the positive and negative values cancel each another out.
This is because x^3 - 5 x is an odd function.
Another function that uses lists as arguments
is the Integrate function.
If you want to compute the definite integral
of e^((-x^2)/2) from -∞ to +∞,
you call the Integrate function
with the function as the first argument..
and then a list
with the variable, lower bound of integration, and upper bound of integration
as the second argument.
The integral is the square root of 2 π.
There are many ways to select certain elements from a list.
To demonstrate,
let’s call the ElementData function to get a list of all chemical elements.
These colored rectangles are how the Wolfram Language displays entities.
Let’s collapse this list so we have room to work.
To find the number of elements in the list,
use the Length function.
There are 118 elements in the periodic table.
..at least when we made this lesson.
To select the first element in the list,
use the First function.
And to get the last element,
use the Last function.
Now suppose you want to select a specific element -
say, the 8th element.
For this, you type in the name of the list, then a double bracket.
Mathematica will change the double brackets to a special symbol
which indicates you are selecting certain parts of the list.
For the 8th element, just type 8 and execute.
And if you want the 8th, 20th, and 92nd elements,
you again use double brackets,
but this time you pass in a list of the elements you want.
An easy way to select consecutive elements
is with the Take function.
Here, the second argument
is how many consecutive elements you want,
starting at the beginning of the list.
If you would like to start at the end and work forwards,
just use a negative integer.
And if you would like to retrieve a consecutive sequence from the middle,
pass in a list with the starting and stopping indices.
Here are the elements 10 through 20.
If you want to use a function to select certain elements,
there is the Select function.
Here, we will create an anonymous function
to find those elements with a half-life of less than 1 year.
When you have several lists,
there are many ways to combine and compare them.
For example,
let’s create a list of the letters in the word Socratica.
And we’ll use the Characters function
to make a list of the letters in the word Mathematica.
By typing the names of these lists and executing
we can see they are lists of characters.
The Join function will concatenate the lists and preserve the order of the elements.
The Union function, however,
will treat the lists as sets, and return an ordered list of the distinct elements.
When we say “distinct elements”, we mean that
duplicates are NOT included in the result.
If you prefer,
you can take the union
by using the mathematical symbol for union.
You enter the union symbol by typing “escape, u-n, escape”.
The result is the same.
If you call the Union function and only provide a single list,
Mathematica will return an ordered set.
Duplicate characters have been removed.
This is an important concept to keep in mind:
in Mathematica, sets are represented by ordered lists.
If you append a duplicate element,
the result is no longer a set, since now there’s a repeated character.
To make this a set once more,
just call the Union function.
You can also take the intersection of lists.
To do this, call the Intersection function
and pass in 2 or more lists.
You can do this symbolically, too.
For the intersection symbol,
type “escape, i-n-t-e-r, escape".
The Wolfram Language uses lists for computation in linear algebra.
For example,
a 2-dimensional vector is just a list of two numbers.
You can perform all the standard vector operations.
You can add vectors..
take the dot product..
multiply a vector by a scalar..
or even find the exact angle between the two vectors.
If you create a pair of 3-dimensional vectors,
you can then find the cross product between them.
And a space-time vector is just a vector with 4 elements.
Matrices in the Wolfram Language are just lists of lists.
Each list is a row of the matrix.
If you would prefer,
you can use the MatrixForm function
to display the matrix in standard mathematical form.
All of the linear algebra operations are available.
You can find the determinant with the Det function.
The inverse is found by raising the matrix to the -1 power
with the MatrixPower function.
You can multiply the matrix by a vector..
And even solve matrix equations.
Everything in Mathematica,
including lists,
are represented by expressions.
To see the underlying expression that the Wolfram Language uses,
call the FullForm function.
This will show you how the expression is actually stored,
rather than how it is rendered for your eyes.
The name of the expression is called the head.
All expressions use square brackets.
The entries inside are called elements.
If you use the double-bracket operator,
you can see the first component is π..
and the second component is 3.
If you ask for the 0th component,
it returns List.
This is because the 0th part of an expression
is always the head of the expression.
Lists are a keystone structure in the Wolfram Language.
They can store sequences and sets of data.
They can represent vectors, matrices, and tensors.
They can be used as arguments for functions like
Sum, Product, and Integrate.
Because of their fundamental nature,
there are over a thousand built-in functions
that operate directly on lists.
Socratica is building a professional course called Mathematica Essentials.
If you are using Mathematica in your research,
or are considering using Mathematica for work
in machine learning, quantitative analysis, scientific research, or more,
then join our Mathematica mailing list
to be notified when the course is available.