Adding named item to named list - guaranteed to append to end of list?
26,926
Solution 1
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)
Solution 2
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.
Related videos on Youtube
Author by
Suraj
Hi! I'm Suraj and here is a little about me, on my blog That! But How?
Updated on July 09, 2022Comments
-
Suraj almost 2 years
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 almost 9 yearsTo 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']]
?
-
-
Suraj over 12 yearsSpacedman - good suggestion to ask R-dev to get it documented. thanks!
-
Carl Witthoft over 12 yearsUmmmm.... 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 over 12 yearsBut 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
-
Spacedman over 12 yearsI 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.
-
Carl Witthoft over 12 yearsSpacedman: 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.
-
smci almost 9 years@CarlWitthoft: the behavior may be different for
tl[['2']]
vstl[[2]]
i.e. string keys (as the OP does) vs numeric keys (as you did) -
baptiste almost 9 years
append(test, list(three=3))
is another option -
smci almost 9 years@SFun28 so what was your answer from R-dev or elsewhere?! It's been 4 years!
-
smci almost 9 years@Ben Bolker it's been 4 years, I figure one of these folks has figured it out, so we just tap into their experience
-
Suraj almost 9 years@smci I don't think I asked on r-devel but it's been a long time so I might just have forgotten
-
smci almost 9 years@SFun28 well has this insert-order behavior been stable over the last few versions, albeit still undefined? (I think it has)
-
Suraj almost 9 years@smci I can't say with confidence because my code doesn't rely on this behavior
-
vladli over 3 years
c(test, one)
copies a list every time. Might be time consuming