Adding named item to named list - guaranteed to append to end of list?

SFun28 picture SFun28 · Sep 29, 2011 · Viewed 22.8k times · Source

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"?

Answer

Joshua Ulrich picture Joshua Ulrich · Sep 29, 2011

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)