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”.

Vectors can be made, by simply hand addition. Examples :

Lists_1.png

Lists_2.png

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 :

Lists_3.png

Lists_4.png

Lists_5.png

Lists_6.png

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

Range [ ] function construct a list according,

Range Lists_7.png]   generates the list Lists_8.png.

Range[Lists_9.png generates the list Lists_10.png.

RangeLists_11.png]   generates the list Lists_12.png with steps di.

Lists_13.png

Lists_14.png

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

Lists_15.png

Lists_16.png

Lists_17.png

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

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

Lists_18.png

Lists_19.png

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

Lists_22.png

Lists_23.png

Lists_24.png

Lists_25.png

Lists_26.png

Lists_27.png

Lists_28.png

or,

Lists_29.png

Lists_30.png

Lists_31.png

Lists_32.png

Lists_33.png

Table[Lists_34.png    uses the successive values Lists_35.png, Lists_36.png, .

Table[

Lists_37.png

Lists_38.png

Prime numbers can be listed

Lists_39.png

Lists_40.png

Lists_41.png

Lists_42.png

Lists_43.png

Lists_44.png

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.

Lists_45.png

Lists_46.png

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 :

Lists_47.png

Lists_48.png

Lists_49.png

Lists_50.png

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.

Lists_51.png

Lists_52.png

in the rnge [0 ,10000]

Lists_53.png

Lists_54.png

We could also use DivisorSigma [ ] function

Lists_55.png

Lists_56.png

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.

Lists_57.png

Lists_58.png

Lists_59.png

Lists_60.png

Another Tact,

Lists_61.png

Lists_62.png

Lists_63.png

Lists_64.png

Lists_65.png

Lists_66.png

Lists_67.png

Lists_68.gif

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.

Lists_69.png

Lists_70.png

Lists_71.png

Lists_72.png

Lists_73.png

Lists_74.png

Lists_75.png

Lists_76.png

Lists_77.png

Lists_78.png

Lists_79.png

Lists_80.png

Tabulating Lists

TableForm

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

Lists_81.png

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.

Lists_82.png

1 2 3
3 4 5

This is the transpose of the data7 table.

Lists_83.png

1 2 3
3 4 5

Table Spacings

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

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

Lists_86.png

Lists_87.png Lists_88.png
Lists_89.png Lists_90.png
Lists_91.png Lists_92.png

Lists_93.png

Lists_94.png Lists_95.png
Lists_96.png Lists_97.png
Lists_98.png Lists_99.png

Lists_100.png

1 3
2 4
3 5

TableAlignments

Lists_101.png

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.

Lists_102.png

Lists_103.png

Lists_104.png

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

Lists_105.png

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

{Vertical,Horizontal} alignments :

Lists_106.png

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

Table Headings

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

Lists_107.png

Lists_108.png

Lists_109.png

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

Lists_110.png

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

Lists_111.png

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

Lists_112.png

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

Lists_113.png

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

Lists_114.png

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 :

Lists_115.png

Lists_116.png

1 2 3
11 12 13

Lists_117.png

Lists_118.png

Lists_119.png

Lists_120.png

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 :

{ Lists_121.png, …, Lists_122.png, { Lists_123.png, …, Lists_124.png }, Lists_125.png, …, Lists_126.png } Apply Lists_127.png, …, Lists_128.png at the beginning, Lists_129.png, …, Lists_130.png at the end, and cyclically the sequence Lists_131.png, …, Lists_132.png in between

• Some typical special cases are as follows :
{Lists_133.png, …, Lists_134.png, { }, Lists_135.png, …, Lists_136.png } Apply the default value in between
{Lists_137.png, …, Lists_138.png, { Lists_139.png, …, Lists_140.png }} Apply cyclically Lists_141.png, …, Lists_142.png through the end
{Lists_143.png, …, Lists_144.png} Apply Lists_145.png, …, Lists_146.png at the beginning and then the default value
{Lists_147.png} Apply Lists_148.png at the beginning and then the default value
{{ Lists_149.png, …, Lists_150.png}} Apply cyclically Lists_151.png, …, Lists_152.png
{{Lists_153.png, Lists_154.png}} Apply cyclically Lists_155.png and Lists_156.png  -that is, alternate between Lists_157.png and Lists_158.png {{Lists_159.png}} or Lists_160.png Apply repeatedly Lists_161.png

Lists_162.png

Lists_163.png

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.

Lists_164.png

Lists_165.png

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 :

Lists_166.png

Lists_167.png


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

Lists_168.png

Lists_169.png


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

Lists_170.png

Lists_171.png


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

Lists_172.png

Lists_173.png


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

Lists_174.png

Lists_175.png


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.

Lists_176.png

Lists_177.png


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.

Lists_178.png

Lists_179.png

Spacings

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

Lists_180.png

Lists_181.png

Lists_182.png

Lists_183.png

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.

Lists_184.png

Lists_185.png

Lists_186.png

Lists_187.png

Lists_188.png

Lists_189.png

Lists_190.png

Lists_191.png

Lists_192.png

Lists_193.png

Lists_194.png

Lists_195.png

Lists_196.png

Lists_197.png

Lists_198.png

Lists_199.png

Lists_200.png

Lists_201.png

Background

Colors of the background.

Lists_202.png

Lists_203.png

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

Lists_204.png

Lists_205.png

Rows with alternating background :

Lists_206.png

Lists_207.png

Lists_208.png

Lists_209.png

Lists_210.png

Lists_211.png

Lists_212.png

Lists_213.png

Frame

Lists_214.png

Lists_215.png

Lists_216.png

Lists_217.png

Lists_218.png

Lists_219.png

Lists_220.png

Lists_221.png

FrameStyle

Lists_222.png

Lists_223.png

Lists_224.png

Lists_225.png

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)

Lists_226.png

Lists_227.png

Lists_228.png

Lists_229.png

Lists_230.png

Lists_231.png

Lists_232.png

Lists_233.png

Lists_234.png

Lists_235.png

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)

Lists_236.png

Lists_237.png

Lists_238.png

Lists_239.png

Lists_240.png

Lists_241.png

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).

Lists_242.png

Lists_243.png

Lists_244.png

Lists_245.png

Lists_246.png

Lists_247.png

Lists_248.png

Lists_249.png

Examples

Example 1

Lists_250.png

Lists_251.png

Lists_252.png

Lists_253.png

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

No need to complicate things :

Lists_254.png

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

Lists_255.png

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 :

Lists_256.png

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

Lists_257.png

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

Lists_258.png

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 :

Lists_259.png

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

Targeting two framed columns : (Always be specific !)

Lists_260.png

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

Targeting a column and a row :

Lists_261.png

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

Targeting two columns and one row :

Lists_262.png

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

Targeting a single one cell :

Lists_263.png

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

Targeting two cells :

Lists_264.png

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

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

Lists_265.png

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 :

Lists_266.gif

Lists_267.gif

Options of the Grid

Row and Column Sums

A new matrix,

Lists_268.png

Lists_269.png

Lists_270.png

Lists_271.png

Lists_272.png

Lists_273.png

Lists_274.png

Then the table :

Lists_275.png

Lists_276.png

Lists_277.png

Lists_278.png

Lists_279.png

Lists_280.png

Lists_281.png

Lists_282.png

Lists_283.png

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:

Lists_284.png

Lists_285.png

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

Lists_286.png

Lists_287.png

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:

Lists_288.png

Lists_289.png

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:

Lists_290.png

Lists_291.png

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:

Lists_292.png

Lists_293.png

Then we draw all interior dividers:

Lists_294.png

Lists_295.png

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

Lists_296.png

Lists_297.png

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

Lists_298.png

Lists_299.png

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

Lists_300.png

Lists_301.png

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

Lists_302.png

Lists_303.png

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):

Lists_304.png

Lists_305.png

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

Lists_306.png

Lists_307.png

Next, we fine|tune the spacings:

Lists_308.png

Lists_309.png

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:

Lists_310.png

Lists_311.png

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)

Lists_312.png

Lists_313.png

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

Lists_314.png

Lists_315.png

Now the first and last column and row are bold :

Lists_316.png

Lists_317.png

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

Lists_318.png

Lists_319.png

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:

Lists_320.png

Lists_321.png

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

Lists_322.png

Lists_323.png

Now we define an alternating background for the rows :

Lists_324.png

Lists_325.png

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

Lists_326.png

Lists_327.png

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

Lists_328.png

Lists_329.png

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

Lists_330.png

Lists_331.png

Lists_332.png

Lists_333.png

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

Lists_334.png

Lists_335.png

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

Lists_336.png

Lists_337.png

Lists_338.png

Lists_339.png

Next, we use both dividers and a frame:

Lists_340.png

Lists_341.png

Frame for a single Item :

Lists_342.png

Lists_343.png

FrameStyle

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

We define some styles for frames :

Lists_344.png

Lists_345.png

Colored Dividers :

Lists_346.png

Lists_347.png

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

Lists_348.png

Lists_349.png

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)

Lists_350.png

Lists_351.png

Lastly we can add a label,

Lists_352.png

Lists_353.png

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.

Created with the Wolfram Language

Geçerli html5