educative.io

Memory usage of updating a list via index and concatenation

In R, I would like to know if updating a List, list via list[i] ← value, would be more memory efficient than via list ← c(list, value) as the latter suggests making a copy of the list, to concatenate then updating the original variable again.
As the List size gets large that would mean making large copies if so.


Course: Learn R from Scratch - Learn Interactively
Lesson: Solution Review: Returning from a Function - Learn R from Scratch

In R, updating a list via list[i] <- value is more memory efficient than via list <- c(list, value). The latter creates a new list and concatenates it with the existing one, requiring additional memory to store the new list, which can be particularly costly when the list size is large. On the other hand, updating a list element directly with list[i] <- value does not create a new list and does not require additional memory.

Happy learning at Educative.