Search code examples
rlistundefined-behaviorinsertionnamed

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


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)