How to Append an Element to a List at Any Position in R

When you don’t know the size of your data, you can use the “list” data structure as you can grow or shrink based on your requirements. In many data operations, we build up results piece by piece, appending them to a list as we go.

To grow the list, you can add an element (numeric value, character vectors, other lists, data frames, or functions) at whichever position you like.

Here are five ways to add an element to a list at any position in R:

  1. Using append()
  2. Using c()
  3. Using the rlist library
  4. Using $ operator
  5. Using the double bracket ( [[ )

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.

Example 1: Adding an element at the end of the list

Visual representation of appending an element to the list using append() function in RAs you can see in this figure, we added an element (4) at the end of the list. The append() method creates a new list instead of modifying in place. If you does not want to modify an original list, you can use this approach as this is 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

Example 2: Appending an element at the specific position

Visual representation of appending an element at specific position in the list

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

With after = 1, you are explicitly telling R to put the new element right after the first item.

Example 3: Appending multiple elements

Visual representation of adding multiple elements to the list

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()

To simultaneously add single or multiple elements to a list, use the c() function. This approach is straightforward but not the most efficient for large lists due to R’s copy-on-modify semantics.

Visual Representation of using c() function to append an element to the list

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.

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

Visual representation of using rlist library to add a new element to the list

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

Screenshot of installing rlist package

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.

Example 1: Adding a new element

Using $ operator to add 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

This approach is still unsuitable for adding new elements instead, you can use the $ operator to modify existing list elements.

Example 2: Modifying an existing element

It modifies the list in place and is efficient for appending to the end. However, it needs careful management of indices.

Visual representation of Modifying an existing element of a list

In the above figure, we modified a list by updating element “b” to 21.

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), as it modifies the existing list.

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.

Example

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), as it modifies the existing list.

Leave a Comment