Lists and List Manipulation in Mathematica

Lists are the main data collection element in Mathematica. They can be used as simple collections of data, as tuples or sets.

In Mathematica, list elements are unordered and can be of any types of data. These data can be numeric and string, image, function, plot or any kind of expression.

In some lists, list elements are not related to each other and they  are simple collections or sets if they do not include any same element only once.

Lists when used as sets, may be generated after some set builder algorithm. In this case the elements of the list are related to each other since they have build according to the same set builder algorithm.

One dimensional lists are called ``vectors”.

The variable data1 is a Mathematica list, but it may also be considered as set, since it does not include any same element more then once.

In Mathematica, there is also different prebuilt functions that can be used for generating sets (which are considered as lists by Mathematica).

One of the very simple set builders is is a function finding the divisors of any integer :

An ordered set is build and stored as variable named ``data2”.

Range [ ] function construct a list according,

Range ]   generates the list .

Range[ generates the list .

Range]   generates the list with steps di.

RandomInteger[ ] and RandomReal [ ] can also construct sets (lists). Uasge of these prebuild functions for making a set with pseudorandom elements :

Table [ ] predefined function is an effective means to devise a set :

Table[expression, n]   Produce a set including n copies of the given expression.

Table[expression with an iterator (let us call i), {i }] generates a list of the values of expr when i runs from 1 to .

or,

Table[    uses the successive values , , .

Table[

Prime numbers can be listed

An advanced way is using user defined functions. One example is given in ``Mathematica Navigator”.

A posxitve integer is a pefect number if it is equal to the sum of its proper divisors. A proper divisor is divisor that is not the number itself. One example of perfect numbers is number 6.

Since the prebuild Divisors [n] function includes the number equal the number itself, if the sum of the divisors (not proper) is equal to 2 n.

Now, the program  given is,

1 stage : testing whether the number is perfect :

This means, screen the numbers from 1 to n, select the numbers which make the defined perfectQ[n_] function true. the symbol # is a placeholder signaled by &. Every tried n takes replaces # and be evaluated. Let us find what are the perfect numbers existing in the range 1 to 100.

in the rnge [0 ,10000]

We could also use DivisorSigma [ ] function

Nested lists

Nested lists (list inside a list) may be generated by simple hand input or by using the Table [ ] function. We will limit ourselves by constructing two elements lists within a list.

Another Tact,

The last way is more flexible and allow to generate a set containing any element pairs at any length.

List Arithmetic

Lists can be added, subtracted, multiplied or divided.

Tabulating Lists

TableForm

TableForm[list]   : Makes a table form a list.

 1 3 2 4 3 5

Table Directions

TableDirections rule is an option for RefLink[TableForm,paclet:ref/TableForm] and RefLink[MatrixForm,paclet:ref/MatrixForm] which specifies whether successive dimensions should be arranged as rows or columns.

TableDirections→Column is the default choice and indicates that the first dimension will be the first element of the columns of the formed table.

We can notice from the new table formed from the list data7 that the first column of the rows of the new formed table are the first elements of the elements of data7. We can change this by adding TableDirections→Column option to the Row and the first row of the new formed table will consist from the first elements of the elements of the data7.

 1 2 3 3 4 5

This is the transpose of the data7 table.

 1 2 3 3 4 5

Table Spacings

Sets the distance between table elements. Examples from Wolfram Docs TableSpacing :

Here, is the place holder of table elements & indicates this and {3,2} specifies 3 rows, 2 columns. Let us see his action :

 1 3 2 4 3 5

TableAlignments

For dimensions that are given as columns, possible alignments are Left, Center and Right, For dimensions that are given as rows, possible alignments are Bottom, Center and Top.

The default is Automatic which is Bottom for rows and Left for columns.

 Item Supplier Number Pencil China 30 Pen US 12 Eraser Turkey 35

 Item Supplier Number Pencil China 30 Pen US 12 Eraser Turkey 35

{Vertical,Horizontal} alignments :

 Item Supplier Number Pencil China 30 Pen US 12 Eraser Turkey 35

Arrange labels to be printed for entries in each dimension of a table or matrix.

 1 2 3 1 Pencil China 30 2 Pen US 12 3 Eraser Turkey 35

 Item Country of Origin Number 1 Pencil China 30 2 Pen US 12 3 Eraser Turkey 35

 Item Country of Origin Number Pencil China 30 Pen US 12 Eraser Turkey 35

Column and Row

Column has the same options as Grid, with the following exceptions.
For Grid, the default value of Alignment is {Center, Baseline}, but for Column the default value is {Left, Baseline}.Thus, the elements in the column are, by default, aligned left. In addition, Column also has the special option ColumnAlignments, with default value Left. However, usually we do not need either Alignment or ColumnAlignments because we can simply add to the command a second argument that indicates the alignment. The alignment can be Left, Center, Right, and "c" (where c is a character).

Column[given list] makes a column from a given list

 {Item,Supplier,Number} {Pencil,China,30} {Pen,US,12} {Eraser,Turkey,35}

 {Item,Supplier,Number} {Pencil,China,30} {Pen,US,12} {Eraser,Turkey,35}

Grid

Tabular representation of data has proved to be an efficient way to transmit information. Mathematica has good tools for preparing tables.TableForm is familiar from earlier versions of Mathematica, whereas Grid, Column, and Row are new in version 6. Grid is versatile and powerful for even complex tables, giving the possibility of detailed formatting.

Grid [ ] function, is another way to generate tables from a data list. Usage :

 1 2 3 11 12 13

Grid Options

General forms of declaring Grid options,

value this option will be applied to all columns.

valueAll

{valuecol}   only for colums

{valuecol  ,  valuerow} apply valuecol for column and valuerow for rows

Values valuecol and valuerow can be a single value or a list of values of the following general form :

{ , …, , { , …, }, , …, } Apply , …, at the beginning, , …, at the end, and cyclically the sequence , …, in between

• Some typical special cases are as follows :
{, …, , { }, , …, } Apply the default value in between
{, …, , { , …, }} Apply cyclically , …, through the end
{, …, } Apply , …, at the beginning and then the default value
{} Apply at the beginning and then the default value
{{ , …, }} Apply cyclically , …,
{{, }} Apply cyclically and   -that is, alternate between and {{}} or Apply repeatedly

Alignment

Horizontal and vertical alignment of items. Default for Rows {Baseline}, for columns {Centered). Other options, {Left}, {Right},{.} (Columns are aligned at the decimal point). In horizontal (columnar) arrangement Left, Right, Centar and “c” (where c is any character) , Vertical (Row weise) alignment Bottom, Center, Baseline and Top.

Alignment→ {Left, Baseline} is a {valuecol  ,  valuerow}  type input. Column will be aligned to the left and rows will be aligned to the baseline.

Dividers

Drawing lines between rows and columns. Default None.

Dividers→{None,All} is a {valuecol  ,  valuerow}  type input. All rows will have dividers columns will not. This  will result as :

Dividers→{All,None} is a {valuecol  ,  valuerow}  type input. All columns will have dividers rows not.

Alignment→ {Left, Baseline} is a {valuecol  ,  valuerow}  type input. All columns and rows will have dividers.

Dividers→All is a valueAll type of input. All columns and rows will have
dividers.

Dividers→{2→ True} is a {valuecol} type of input. That will only affect columns and trace a divider after 2nd. column :

Dividers→{None,2→ True} is a {valuecol, valuerow} type of input. Columns won’t have dividers but one divider will be drawn after 2nd. row.

Dividers→{2→ True , 2→ True} is a {valuecol, valuerow} type of input. One divider will be drawn after 2nd. column, the other at 2nd. row.

Spacings

Spacings is distance in units of current font size, between rows and columns. Automatic , usually means {0.7, 0.4}

We can see the effect of spacing rule.

ItemStyle

One of the exciting options. ItemStyle arrange the embellishments of the row and columns. Examples illustrates the usage of this option.

Background

Colors of the background.

GrayLevel[0.7, 0.3] means, gray at level 0.7 with opacity 0.3.

Rows with alternating background :

Frame

FrameStyle

ItemSize

Width and height of each item; examples of values: Automatic (separately size items to fit within the total formatting width; long items may take several lines), All (make all items the same width and height), Full (allow each item its full width and height; long items are not divided into several lines), w (give all items width w, measured in ems), {w, h} (give all items width w and height h, with h measured in line heights)

BaseLine

Base style specifications for the grid; examples of values : {}, Blue (all items are blue), Directive[Red, Bold, Italic] (all items are red, bold, and italic)

BaselinePosition

How the grid should be positioned inside text; examples of values: Automatic (means Axis),
Axis (axis of the middle row in the grid),
Baseline (baseline of the middle row in the grid),
Bottom (bottom of the whole grid),
Center (halfway from top to bottom),
Top (top of thewhole grid).

Examples

Example 1

 1 2 3 4 5 6 7 8 9 1 11 12

No need to complicate things :

 1 2 3 4 5 6 7 8 9 1 11 12

 1 2 3 4 5 6 7 8 9 1 11 12
 1 2 3 4 5 6 7 8 9 1 11 12

Now for rows :

 1 2 3 4 5 6 7 8 9 1 11 12

 1 2 3 4 5 6 7 8 9 1 11 12

 1 2 3 4 5 6 7 8 9 1 11 12

Exceptional Values

If we have some exceptional formatting for some columns, rows, or items, we can add definitions for these cases as rules.

Exceptional values can be given as rules (a single rule or a list of rules) :
{rulesj} Apply rulesj for specific columns
{rulesj, rulesi} Apply rulesj for specific columns and rulesi for specific rows
{valc, rulesj} Apply valc for columns but rulesj to specific columns
{{valc, rulesj}, {valr, rulesi}} Apply valc for columns and valr for rows but rulesj to
specific columns and rulesi to specific rows
{valc, valr, rulesij} Apply valc and valr for columns and rows but rulesij for specific items
(not applicable for Dividers and Spacings)

Here, rulesj may be, for example, 4 → True, meaning that the value True is used for the fourth column.
Similarly, rulesi may be, for example, 3  → Green, meaning that the value Green is used for the third row.Also, rulesij may be, for example, {2, 5}  → Red, meaning that the value Red is used for the item at position (2, 5).
In addition, rulesij may define a range of items. An example is {{2, 4}, {1, 3}}  → Red, meaning that the value Red is used for items with row index in {2, 4 } and column index in {1, 3 } .

More for frames,

Targeting only one framed column :

 1 2 3 4 5 6 7 8 9 1 11 12

Targeting two framed columns : (Always be specific !)

 1 2 3 4 5 6 7 8 9 1 11 12

Targeting a column and a row :

 1 2 3 4 5 6 7 8 9 1 11 12

Targeting two columns and one row :

 1 2 3 4 5 6 7 8 9 1 11 12

Targeting a single one cell :

 1 2 3 4 5 6 7 8 9 1 11 12

Targeting two cells :

 1 2 3 4 5 6 7 8 9 1 11 12

Targeting a {{column cell},{row cell}} region : {valcol, valrow, rulesij}

 1 2 3 4 5 6 7 8 9 1 11 12

Row indices 2 to 3 , column indices 2 to 4.

The grid may also contain text and graphics :

Options of the Grid

Row and Column Sums

A new matrix,

Then the table :

 Rows Col 1 Col 2 Col 3 Col 4 Sums Row 1 6 21.2 3.05 64.2 94.45 Row 2 34 9.582 143.17 8.702 195.454 Row 3 985 0.6914 70.4 126.6 1182.69 Sums 1025 31.4734 216.62 199.502 1472.6

Here the transpose may be written as Esc + tr + Esc

Enhancing the Grid

Alignment

** Alignment   Horizontal and vertical alignment of items; examples of values: {Center, Baseline}
(columns are centered, rows are at baseline), Left (columns are aligned left), Right (columns are
aligned right), “.” (columns are aligned at the decimal point). In horizontal (or column) alignment,
we can use Left, Center, Right, and “c” (where c is a character). In vertical (or row) alignment,
we can use Bottom, Center, Baseline, and Top.

As an example, we consider the table t we formed previously. First, we align the columns at the right:

Right alignment is good for integers. Then we align the columns at the decimal point :

Columns with integers are seemingly untouched, but column with a decimal point are aligned with the decimal point. In the reality integers are considered as having a decimal zero and aligned with it. Note that the column headers are aligned such that they end at the position of the decimal point, and this causes the headers to be too far to the left. For the row headers, the right alignment is not good.

Now we align the first column left, the second column right, and the rest of the columns at the
decimal point:

Note that here the value of the option is of the form {valcol} mentioned previously in a box. This form of the option means that valcol is applied for columns. Here, valcol is {Left, Right, {“.”}} and this is of
the general form {a1, a2, {b1}} mentioned before. This form of the option means that a1
is applied for the first column, a2 for the second column, and b1 for the rest of the columns.

The previous table is quite good. However, the column headers should be aligned better. We define
these exceptional alignments as rules:

Here, the value of the option is of the form {valcol, valrow, rulesij} mentioned previously in a box. This
form of the option means that valcol is applied for columns, valrow for rows, and rulesij for special
items. Actually, valrow or Baseline is the default for rows but we have to define something for the rows
so that we can define rules for single items. The rule {{1, 1}, {2, 6}} → Center defines that the column
alignment should be Center for items with row indices in the range {1, 1} and column indices in the
range {2, 6}.

Dividers

Dividers Where to draw lines; examples of values : None, All (all items become boxed), Center
(all interior dividers), {None, All} (no column lines, all row lines, lines also before the first row and after the last row), {2 →  True} (a line after the first column), {None, 2→ True} (a line below the first row), {2 →  True, 2 →  True} (a line after the first column and below the first row)

Note that if we give Dividers a list of values, the first value corresponds with the divider before the first column or above the first row. The last value that can be given concerns the divider after the last column or below the last row.

First, we draw all dividers:

Then we draw all interior dividers:

Next, we ask for no column dividers and all row dividers:

Next, we ask for no row dividers and all column dividers:

Next, we add, for both the columns and the rows, the second divider:

Now we add, for columns, the second divider and the next-to-last divider:

Spacings

Spacings Space between columns and rows (in units of the current font size); examples of values : Automatic (usually means {0.7, 0.4} : the space between columns is 0.7 and that between rows is 0.4), 1.2 (the space between columns is 1.2 and the space between rows is the default 0.4)

Note that if we give Spacings a list of values, the first value corresponds with the space before the first column or above the first row, and the last value that can be given concerns the space after the last column or below the last row. However, the first or last value does not have any effect unless there is a frame line or divider line before the first column or row or after the last column or row.

We make the space between columns 2 (the space between rows is the default 0.4):

Now make the space between columns 2 and the space between rows 0.1:

Next, we fine|tune the spacings:

Here, the space between columns is 1.5. The space below the first row and above the last row is 1; for the rows in between, we use the space of 0.5.

Note that Mathematica assumes that the spacings begin above the first row and extend below the last row. Thus, to get the space 1 after the first row and above the last row, we have to define a space also above the first row and below the last row. We used the value 0 for these spaces, but in this example these values do not have any effect.

Note that Mathematica assumes that the spacings begin above the first row and extend below the last row. Thus, to get the space 1 after the first row and above the last row, we have to define a space also above the first row and below the last row. We used the value 0 for these spaces, but in this example these values do not have any effect.

If we have a frame or dividers above the first row and below the last row, then the first and last values of Spacings do have an effect because these spacings define how much space should be around the first and last divider. Here, we use the value 1 for these spacings:

ItemStyle

ItemStyle Styles of columns and rows; examples of values: None, Blue (all items are blue), {1 → Red} (the first column is red), {Automatic, 1 →  Bold} (the first row is bold), {1 →  Directive[Red, Bold, 14]} (the first column is red, bold, and size 14), {1 →  Bold, 1 →  Bold} (the first column and the first row are bold)

Then we use the bold style for both the first column and the first row :

Now the first and last column and row are bold :

Next, we use both the bold style and a color:

Background

Background Colors of the background; examples of values: None, GrayLevel[0.9] (all items are gray), {Automatic, {{ White, LightGray}}} (columns are default, rows alternate between white

Define a gray background for the table:

Next, we define a gray background for the first row (GrayLevel 0.9, Opacity 0.5 :

Now we define an alternating background for the rows :

Define then the first and last rows to be light red :

Next, we define the background for a range of items or for a singe item :

{ Automatic, Automatic, {{{5, 5}, {2, 5}} → Darker[Yellow, 0.1], {{2, 4}, {6, 6 }} → Darker[Yellow, 0.1], {5, 6} → Red}}

Frame

Where to draw frames; examples of values: None, True (frame around the whole grid), All (all items become boxed), {All} (frame around each column), {None, All} (frame around each row)

Add frame for all the table

Add a frame for all items (this can also be obtained with Dividers → All) :

Next, we use both dividers and a frame:

Frame for a single Item :

FrameStyle

Style of frames and dividers; examples of values: Automatic, Red, Directive[Gray,Thickness[2]]

We define some styles for frames :

Colored Dividers :

We can get rid of the dashing and get straight line dividers.

ItemSize

Width and height of each item; examples of values: Automatic (separately size items to fit within the total formatting width; long items may take several lines), All (make all items the same width and height), Full (allow each item its full width and height; long items are not divided into several lines), w (give all items width w, measured in ems), {w, h} (give all items width w and height h, with h measured in line heights; the default value of h is 1)

Lastly we can add a label,

Tables in Mathematica may be somehow difficult to master. The aim of these examples is to provide some working media which may suit to somebody’s needs and use it without hassles of the Mathematica language.

These material is largely based on Mathematica Navigator version 3.1 targeting Mathematica Releases 7 and up. Most material is revised and reshaped for to be more comprehensive. Mathematica Navigator is perhaps largest and well organised textbook with countless examples, very advisable for learning Mathematica.

I hope that this manuscript will be useful for whom searching a shortest ways to present Mathematica materials.