Surprising behavior of Part[ ]

Compare:

17[[All]]
(* Integer[] *)

The evaluation of expr[[All]] should result in h[p1, p2,...] where h is Head[expr] and p1, p2,... is a possibly empty list (not List) of all the parts of expr. An Integer has no parts (because it is atomic), but its head is Integer. Hence we get Integer with an empty argument list.


Consider the following example:

We have a list of 3-tuples:

list = {{1,2,3}, {5,4,3}, {9,7,5}, {0,3,8}}

Take the first element of each sublist.

list[[All, 1]]
(* {1,5,9,0} *)

If the list had 4 elements, the result will have 4 elements too. In general, if the list has $n$ elements, the result should also have $n$ elements. But what if the list is empty, i.e. $n=0$? It would be reasonable to get a result with 0 elements, of course. But for that to work, Part needs to be a bit flexible, after all, we can't take the 1st element of nothing ... or can we?

list = {}

list[[All, 1]]
(* {} *)

Well, it works. And it is extremely useful because I do not need to have extra code for these special cases.

This explains why it is okay to add more indexes to Part than the number of levels that the expression has. This is why you can have 10 Alls in there. But you could also have list[[All, All, 1, 1]], not just list[[All, All, All, All]]


As for why 1[[All]] is Integer[], Michael has already explained it. My answer is only for explaining why you can have many Alls in there.