Skip to main content
added 17 characters in body
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

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 34 elements, the result has 3will 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.

Consider the following example:

We have a list of 3-tuples:

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

Take the first element of each sublist.

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

If the list had 3 elements, the result has 3 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.

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.

Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

Consider the following example:

We have a list of 3-tuples:

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

Take the first element of each sublist.

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

If the list had 3 elements, the result has 3 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.