Lists
Lists
In doing calculations, it is often convenient to collect together several objects, and treat them as a single entity. Lists give you a way to make collections of objects in the Wolfram Language. As you will see later, lists are very important and general structures in the Wolfram Language.
A list such as {3,5,1} is a collection of three objects. But in many ways, you can treat the whole list as a single object. You can, for example, do arithmetic on the whole list at once, or assign the whole list to be the value of a variable.
This takes differences between corresponding elements in the two lists. The lists must be the same length:
You can apply any of the mathematical functions in "Some Mathematical Functions" to whole lists:
We first encountered lists in "Making Lists of Objects" as a way of collecting numbers together. Here, we shall see many different ways to use lists. You will find that lists are some of the most flexible and powerful objects in the Wolfram Language. You will see that lists in the Wolfram Language represent generalizations of several standard concepts in mathematics and computer science.
At a basic level, what a Wolfram Language list essentially does is to provide a way for you to collect together several expressions of any kind.
The mathematical functions that are built into the Wolfram Language are mostly set up to be "listable" so that they act separately on each element of a list. This is, however, not true of all functions in the Wolfram Language. Unless you set it up specially, a new function f that you introduce will treat lists just as single objects. "Applying Functions to Parts of Expressions" and "Structural Operations" will describe how you can use Map and Thread to apply a function like this separately to each element in a list.
You can use lists as tables of values. You can generate the tables, for example, by evaluating an expression for a sequence of different parameter values.
Table uses exactly the same iterator notation as the functions Sum and Product, which are discussed in "Sums and Products":
You can perform other operations on the lists you get from Table:
TableForm displays lists in a "tabular" format. Notice that both words in the name TableForm begin with capital letters:
All the examples so far have been of tables obtained by varying a single parameter. You can also make tables that involve several parameters. These multidimensional tables are specified using the standard Wolfram Language iterator notation, discussed in "Sums and Products".
The table in this example is a list of lists. The elements of the outer list correspond to successive values of
. The elements of each inner list correspond to successive values of
, with
fixed.
Sometimes you may want to generate a table by evaluating a particular expression many times, without incrementing any variables.
This gives a list of four pairs of numbers sampled from {1,2,3,4}. Table reevaluates RandomSample[{1,2,3,4},2] for each element in the list, so that you get four different samples:
You can use Table to generate arrays with any number of dimensions.
| Table[f,{imax}] | give a list of imax values of f |