Adding named item to named list - guaranteed to append to end of list?
-
05-02-2021 - |
Question
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"?
Solution
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)
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow