List

{e1,e2,}

is a list of elements.

Details

  • Lists are very general objects that represent collections of expressions.
  • Functions with attribute Listable are automatically "threaded" over lists, so that they act separately on each list element. Most builtin mathematical functions are Listable.
  • {a,b,c} represents a vector.
  • {{a,b},{c,d}} represents a matrix.
  • Nested lists can be used to represent tensors.
  • If Nothing appears in a list, it is automatically removed.
  • Parallelize[{e1,e2,}] evaluates the elements e1, e2, in parallel. »

Background & Context

  • List is a very general construct used to represent collections of expressions. Lists may have any length or depth. The expression List[a,b,c,] is commonly written and displayed using the shorthand syntax {a,b,c,}. Lists are particularly important in the Wolfram Language, which does not define explicit vector, matrix, tensor, etc. objects but rather uses (possibly nested) lists to represent such structures. For example, {a,b,c,} can represent a vector, {{a,b},{c,d}} a matrix, and so on.
  • Functions with attribute Listable are automatically threaded over lists, meaning they act separately on each list element. Most builtin mathematical functions are Listable.
  • Apply replaces the head of a List (or any other expression) with a new head, while Map applies a function to elements on the first level of a List (or any other expression).
  • SparseArray may be used to efficiently represent and compute with lists (or nested lists) that have a constant (often 0) background value. A SparseArray can be expanded to a full-dimensional List using Normal.
  • Values of a list can be efficiently modified in place using Set, e.g. list[[k]]=newValue. Common operations to access, insert, or delete elements of a list include Part, Take, Drop, Extract, Insert, Delete, PadLeft/PadRight, Append/AppendTo, and Prepend/PrependTo.
  • A flat list of values (i.e. a vector) may be plotted using ListPlot, and an array of values given by a rectangular list of lists may be plotted using ArrayPlot, MatrixPlot, ListDensityPlot, or related functions. Other important and useful functions commonly applied to lists include Total, Accumulate, Mean, and ListConvolve.
  • Association provides a generalization of symbolically indexed lists, associative arrays, dictionaries, hashmaps, structs, and a variety of other powerful data structures. An Association is so named because it associates keys with values, allowing highly efficient lookup and updating even with millions of elements.
  • A list can be converted to a sequence of expressions by applying Sequence to it. This can be particularly useful since functions in the Wolfram Language often take a flat sequence of arguments instead of an argument list, so use of Sequence allows list-represented data to be easily spliced into other functions.

Examples

open allclose all

Basic Examples  (1)

The short notation {} and the FullForm notation List[] are equivalent:

Scope  (31)

Representation of Vectors, Matrices, and Other Arrays  (4)

A vector is a list of nonlist elements:

Many operations work on vectors, like Dot and Norm:

A matrix is a list of vectors of equal length:

Many operations work with matrices, like Dot, Transpose, and Det:

A rectangular array is represented by nested lists with consistent dimensions:

Many operations work on arrays of any depth, like Dot and Fourier:

The three-dimensional discrete Fourier transform:

Ragged arrays that are not rectangular can also be used:

Many structural functions will work with ragged arrays:

If the elements are at the same depth, you can use PadRight to make a rectangular array:

Constructing Lists  (5)

Range constructs a list consisting of a range of values:

Array constructs lists using a function:

When given multiple dimensions, matrices or deeper arrays are constructed:

Table constructs lists using an expression and an iterator:

When given multiple iterators, matrices and arrays can be constructed:

Functional commands like NestList create lists of the results:

To construct a list when the length is not known ahead of time, Sow and Reap are efficient:

Some trials of rolling a die until the same number comes up twice in a row:

Listable Functions  (4)

Add two vectors:

Scalar multiple:

Sine of a vector:

Scalar multiple of a matrix:

Matrix plus a vector adds the component of the vector to the rows of the matrix:

Function applied element-wise to a matrix:

Any function that has the Listable attribute will thread over lists element-wise:

Use Threaded to alter how listable functions combine arguments:

Operations on List Elements  (5)

Apply makes the elements of a list the arguments of a function:

If you have a nested list, applying at level 1 gives a list f applied to the sublists:

Map applies a function to the elements of a list:

For a nested list, Map can apply f at any level or multiple levels:

Do, Product, Sum, and Table can iterate over a list:

Part can be used to get elements of lists:

You can get multiple parts by specifying a list of parts:

Or by using Span:

Use Outer to apply a function to elements of multiple lists:

Combining Lists  (4)

Use Join to combine two lists end to end:

Use Splice to insert elements of one list as individual elements of another list:

A combination of Sequence and Apply can be used for the same effect:

Unlike Sequence, Splice[list] is inert inside other functions:

Use Insert to place a whole list as a single element inside another list:

Append behaves similarly:

As does Prepend:

Use Flatten to remove inner lists:

Lists as Finite Sets  (2)

Complement, Union, and Intersection treat List as a set:

Construct various combinatorial structures using Subsets, Tuples, and IntegerPartitions:

Lists as Control Structures  (2)

Many commands use {var, vmin, vmax} as a specification of variable range:

Many commands use {v1,v2,} for a collection of variables:

Lists of Rules  (2)

A list of rules is returned as a solution by many solving commands:

You can use the values of the results with ReplaceAll:

When multiple solutions are possible, the result is a list of rule lists:

When a list of rule lists is used in ReplaceAll, you get a list of results:

Even if there is only one solution, the extra List is used for consistent structure:

Lists of Data  (3)

Lists are very good for holding data since the elements can be anything:

Sine of successive squares:

Plot the data:

Data from a function sampled at points in two dimensions:

A piecewise polynomial that interpolates the data:

Plot the InterpolatingFunction:

Plot the data directly:

Properties & Relations  (6)

Like all Wolfram Language expressions, lists are 1-indexed:

In most format types, including InputForm, lists are displayed as {}:

FullForm treats lists like any other expression, displaying them as List[]:

This makes it clear that lists have head List:

Sequence is automatically spliced into lists:

This is a particular case of the general behavior of Sequence:

Nothing is automatically removed from lists:

This behavior is specific to lists:

A SparseArray represents a list:

They are Equal:

They can be equivalently used in many commands:

They are not identical because the representation is different:

Normal[slist] gives the List representation:

Parallelize[list] evaluates the elements of list in parallel:

Wolfram Research (1988), List, Wolfram Language function, https://reference.wolfram.com/language/ref/List.html (updated 2014).

Text

Wolfram Research (1988), List, Wolfram Language function, https://reference.wolfram.com/language/ref/List.html (updated 2014).

CMS

Wolfram Language. 1988. "List." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/List.html.

APA

Wolfram Language. (1988). List. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/List.html

BibTeX

@misc{reference.wolfram_2023_list, author="Wolfram Research", title="{List}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/List.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2023_list, organization={Wolfram Research}, title={List}, year={2014}, url={https://reference.wolfram.com/language/ref/List.html}, note=[Accessed: 19-March-2024 ]}