26

When adding a named item to a list, is it guaranteed that the item will be added to the end of the list? In practice it appears to be the case, but not sure if this is a dangerous assumption?

test = list()
test[[ "one" ]] = 1
test[[ "two" ]] = 2  # will always appear after "one"?
test[[ "three" ]] = 3  # will always appear after "two"?
smci
  • 32,567
  • 20
  • 113
  • 146
Suraj
  • 35,905
  • 47
  • 139
  • 250
  • To be clear, you're using a string key `[['one']]` or `[['1']]` not a numeric key `[[1]]`. And can we take it all your string keys are in a collating order? Can you do string keys like `[['1']]` instead of `[['one']]` ? – smci Jul 27 '15 at 01:27

2 Answers2

38

If it's not documented (and it doesn't appear to be), then I wouldn't rely on it. You can ensure it appears at the end of the list by doing something like:

test <- list()
test <- c(test, one=1)
test <- c(test, two=2)
test <- c(test, three=3)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 2
    But be very careful how you add stuff to a list. look at the result of `Rgames: tl <- list()` `Rgames: tl[[1]]<- list(one=1)` `Rgames: tl[[2]]<- data.frame(two=2)` `Rgames: tl$three <- 3` – Carl Witthoft Sep 29 '11 at 17:01
  • @CarlWitthoft: the behavior may be different for `tl[['2']]` vs `tl[[2]]` i.e. string keys (as the OP does) vs numeric keys (as you did) – smci Jul 27 '15 at 01:26
  • 5
    `append(test, list(three=3))` is another option – baptiste Jul 27 '15 at 01:29
  • 1
    `c(test, one)` copies a list every time. Might be time consuming – vladli Dec 23 '20 at 12:28
5

I suspect if you delved into the C code of R then you'd see that it was true, but as Joshua says, its not documented. You could ask on R-dev for an opinion on whether such behaviour should be documented. There may already be existing code that depends on it.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 3
    Spacedman - good suggestion to ask R-dev to get it documented. thanks! – Suraj Sep 29 '11 at 15:37
  • 3
    Ummmm.... I'm hard-pressed to see how new elements of a list (or a dataframe, or a vector for that matter), could be put anywhere BUT at the "end." The moment you define an element, it gets assigned an unused location. Emphasis on "unused." This should be just as obvious as the fact that adding, e.g., a third item to a list with two elements will NOT get it assigned to the 47-th location. – Carl Witthoft Sep 29 '11 at 16:54
  • 4
    I agree that putting an element in the 7th place of a length-12 list would be perverse, but why the end, and not the beginning? When you add an element by name you aren't giving any positional information, so unless documented otherwise the language is free to put it anywhere. Some languages might end up ordering it by the hash value of the string key. – Spacedman Sep 29 '11 at 18:44
  • Spacedman: Maybe, but in all honesty I have not seen this behavior in any math/stat pack I've used, and would be rather shocked if any group, especially one as learned as the R-core folks :-) , were to do what you suggest. R's syntax is painstakingly designed to be as intuitive as possible. – Carl Witthoft Sep 29 '11 at 19:34
  • 1
    @SFun28 so what was your answer from R-dev or elsewhere?! It's been 4 years! – smci Jul 27 '15 at 01:29
  • @Ben Bolker it's been 4 years, I figure one of these folks has figured it out, so we just tap into their experience – smci Jul 27 '15 at 02:19
  • @smci I don't think I asked on r-devel but it's been a long time so I might just have forgotten – Suraj Jul 28 '15 at 16:56
  • @SFun28 well has this insert-order behavior been stable over the last few versions, albeit still undefined? (I think it has) – smci Jul 28 '15 at 21:22
  • @smci I can't say with confidence because my code doesn't rely on this behavior – Suraj Aug 03 '15 at 00:24