To grow the list, you can add an element (numeric value, character vectors, other lists, data frames, or functions) at whichever position you like. You can add a single element, multiple elements, or a list as an element. The final way depends on the user’s intention.
Here are five ways to append an element to a list:
- Using append()
- Using c()
- Using the rlist library
- Using $ operator
- Using the double bracket ( [[ ) (index assignment)
Method 1: Using append()
If you are picky about which position you want to add an element, append() is the method for you. It lets you add an element at the end of the list but provides an argument “after” through which you can add an element anywhere.
It is like a magic wand in your hand that lets you place your new element exactly where you want it in the list.
Adding an element at the end of the list
The append() method creates a new list instead of modifying in place. If you do not want to modify an original list, you can use this approach as this is a built-in function.
list1 <- list(a = 1, b = 2, c = 3)
list2 <- list(d = 4)
appended_list <- append(list1, list2)
print(appended_list)
Output
$a
[1] 1
$b
[1] 2
$c
[1] 3
$d
[1] 4
Appending an element at the specific position
Here, we added an element (4) after the position 1 using the “after” parameter.
list1 <- list(1, 2, 3)
list2 <- append(list1, 4, after = 1)
print(list2)
Output
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 4
[[4]]
[1] 3
Appending multiple elements
The above figure shows that we added 3 elements (4, 5, 6) at the end of the list.
list1 <- list(1, 2, 3)
elements <- c(4, 5, 6)
multi_list <- append(list1, elements)
print(multi_list)
Output
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
[[5]]
[1] 5
[[6]]
[1] 6
Time complexity: O(n), where n is the number of elements in the list.
Space complexity: O(n), as it creates a new list.
Method 2: Using c()
You can combine the original list with a new element using the c() function. It will help you add single or multiple elements to a list simultaneously. This approach is straightforward but not the most efficient for large lists due to R’s copy-on-modify semantics.
The above figure shows that we appended element 4 at the end of the position.
list1 <- list(1, 2, 3)
list1 <- c(list1, 4)
print(list1)
Output
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
You can use this approach when you need fine-grained control over the insertion position.
If the new element is a list and you want it nested as a single item, wrap it in list().
Time complexity: O(n), where n is the number of elements in the list.
Space complexity: O(n), as it creates a new list.
Method 3: Using the ‘rlist’ library
The above figure shows that we used the rlist library’s list.append() function to add an element (4) at the end of the list highlighted by the red marker.
For this practical, you need to install the “rlist” library. Use the below command to install it.
install.packages("rlist")
library("rlist")
list1 <- list(a = 1, b = 2, c = 3)
new_list <- list.append(list1, d = 4)
print(new_list)
Output
$a
[1] 1
$b
[1] 2
$c
[1] 3
$d
[1] 4
Method 4: Using $ operator
The $ operator is not typically used for adding new elements but can be used for modification. However, if the name provided after the $ does not exist in the list, it will add a new element with that name.
Adding a new element
list1 <- list(a = 1, b = 2, c = 3)
list1$d <- 4
list1
Output
$a
[1] 1
$b
[1] 2
$c
[1] 3
$d
[1] 4
Modifying an existing element
list1 <- list(a = 1, b = 2, c = 3)
list1$b <- 21
cat("After modifying the list", "\n")
print(list1)
Output
After modifying the list
$a
[1] 1
$b
[1] 21
$c
[1] 3
Time complexity: O(1) for appending to the end, O(n) for inserting in the middle.
Space complexity: O(1)
Method 5: Using the double bracket [[
The double bracket ([[) approach is more direct than the c() function and avoids some copying overhead, which is especially helpful in any type of loop.
list1 <- list(1, 2, 3)
list1[[length(list1) + 1]] <- 4
cat("After adding an element the list", "\n")
print(list1)
Output
After adding an element the list
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
The output shows that the element “4” is added at the end of the list.
Time complexity: O(1) for appending to the end, O(n) for inserting in the middle.
Space complexity: O(1)
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.