You have to return and assign the modified list:
a <- list(x=4, y=2)
addNew <- function(l) {
l$c <- 7
return(l)
}
a <- addNew(a)
EDIT: As @mitra mentioned you could use <<-
:
a <- list(x=4, y=2)
addNew <- function() {
a$c <<- 7
}
But that changes the typical behaviour of a function in R. Most user didn't expect that a function has side effects like changing the global environment.
Please read Patrick Burns' R Inferno (p. 35):
If you think you need <<- , think again. If on reflection you still think you
need <<- , think again. Only when your boss turns red with anger over you
not doing anything should you temporarily give in to the temptation. There
have been proposals (no more than half-joking) to eliminate <<- from the
language. That would not eliminate global assignments, merely force you to use
the assign function to achieve them.