General explanation
First, in general, a valid expr[[i1, i2,..., in]]]
requires expr
to have at least n
levels, and the result will have n
fewer levels.
The OP's example list {{1, 2}, {2, 4}, {3, 6}, {4, 8}}
has two. The code
{{1, 2}, {2, 4}, {3, 6}, {4, 8}}[[;; , 1, 1]]
needs at least three levels. Hence the error.
Next, in the sentence of the documentation for Part
,
expr[[i,j,...]] or Part[expr,i,j,...]] is equivalent to expr[[i]][[j]]...
the i
, j
, ... are to be integers, or so I used to think. However, as @kguler said in a comment, the equivalence holds if expr[[i, j,...]]
is valid.
If instead of an integer, a Span
or a List
(or All
) is used, the result will be a (possibly nested) list. Each Span
and List
adds a level to the depth of the result one would have had if every part had been a simple integer. This allows for further extraction of parts as in the OP's first example.
OP's first example
We can break down the OP's example as follows. The expression
{{1, 2}, {2, 4}, {3, 6}, {4, 8}}[[;; , 1]][[1]]
is equivalent to the last result of
{{1, 2}, {2, 4}, {3, 6}, {4, 8}}[[;; , 1]]
%[[1]]
(*
{1, 2, 3, 4}
1
*)
The first expression has a Span
and evaluates to a list of parts, the first part of each expression at level 2. Adding a [[1]]
extracts the first part of this list.
It may be a little easier to see what's going on in an example that gives no errors:
foo = ArrayReshape[Range@8, {4, 2, 1}]
foo[[;; , 1, 1]]
foo[[;; , 1]][[1]]
(*
{{{1}, {2}}, {{3}, {4}}, {{5}, {6}}, {{7}, {8}}}
{1, 3, 5, 7}
{1}
*)
The last output is a list because {1}
is the first part of
foo[[;; , 1]]
(*
{{1}, {3}, {5}, {7}}
*)
Span
threads over the defined elements, which then do not have sufficient depth for the last part specification. E.g.{{1, 2}, {2, 4}, {3, 6}, {4, 8}}[[1, 1, 1]]
returns the same error (on probably any version). Note: I removed thebugs
tag, which is for confirmed bugs only. $\endgroup$expr[[i,j,...]]
is a valid expression, it is equivalent to ..."? $\endgroup$Part
+Span
evaluation. That might actually be quite useful! $\endgroup$