Menu
Coddy logo textTech

Modifying Lists

Part of the Fundamentals section of Coddy's R journey — lesson 73 of 78.

Now that you know how to create lists and access their elements, let's learn how to modify them. You can change existing elements, add new ones, or remove elements entirely.

To change an existing element, use double brackets with assignment:

my_list <- list(10, "hello", TRUE)
my_list[[2]] <- "world"
print(my_list)

Output:

[[1]]
[1] 10

[[2]]
[1] "world"

[[3]]
[1] TRUE

To add a new element, simply assign to an index that doesn't exist yet:

my_list <- list(10, "hello")
my_list[[3]] <- FALSE
print(my_list)

Output:

[[1]]
[1] 10

[[2]]
[1] "hello"

[[3]]
[1] FALSE

To remove an element, assign NULL to it:

my_list <- list(10, "hello", TRUE)
my_list[[2]] <- NULL
print(my_list)

Output:

[[1]]
[1] 10

[[2]]
[1] TRUE

Notice that after removing the second element, the remaining elements shift to fill the gap. The element that was at position 3 is now at position 2.

challenge icon

Challenge

Easy

You will receive three lines of input:

  1. Three comma-separated values to initialize a list (e.g., 50,hello,TRUE)
  2. An index and a new value separated by a comma for modification (e.g., 2,goodbye)
  3. An index of the element to remove (e.g., 3)

Perform the following operations:

  1. Create a list with the three initial values (convert them to appropriate types: numeric, character, logical)
  2. Print the original list
  3. Modify the element at the specified index with the new value (keep it as a character string)
  4. Print the list after modification
  5. Add a new fourth element to the list with the value 999
  6. Print the list after adding the new element
  7. Remove the element at the specified index (from the third input)
  8. Print the final list

For example, if the inputs are:

100,world,FALSE
1,updated
2

The output should be:

[[1]]
[1] 100

[[2]]
[1] "world"

[[3]]
[1] FALSE

[[1]]
[1] "updated"

[[2]]
[1] "world"

[[3]]
[1] FALSE

[[1]]
[1] "updated"

[[2]]
[1] "world"

[[3]]
[1] FALSE

[[4]]
[1] 999

[[1]]
[1] "updated"

[[2]]
[1] FALSE

[[3]]
[1] 999

Explanation:

  • Original list: 100, "world", FALSE
  • After modifying index 1: "updated", "world", FALSE
  • After adding fourth element: "updated", "world", FALSE, 999
  • After removing index 2: "updated", FALSE, 999 (remaining elements shift)

Remember to use double brackets [[ ]] for modifications and assign NULL to remove elements.

Cheat sheet

To modify an existing element in a list, use double brackets with assignment:

my_list <- list(10, "hello", TRUE)
my_list[[2]] <- "world"

To add a new element to a list, assign to an index that doesn't exist yet:

my_list <- list(10, "hello")
my_list[[3]] <- FALSE

To remove an element from a list, assign NULL to it:

my_list <- list(10, "hello", TRUE)
my_list[[2]] <- NULL

When an element is removed, the remaining elements shift to fill the gap. The element that was at position 3 becomes position 2.

Try it yourself

# Read input
con <- file("stdin", "r")
initial_values <- suppressWarnings(readLines(con, n = 1))  # Three comma-separated values
modify_input <- suppressWarnings(readLines(con, n = 1))    # Index and new value
remove_index <- suppressWarnings(readLines(con, n = 1))    # Index to remove
close(con)

# Parse the initial values
values <- strsplit(initial_values, ",")[[1]]
val1 <- as.numeric(values[1])
val2 <- values[2]
val3 <- as.logical(values[3])

# Parse modification input
mod_parts <- strsplit(modify_input, ",")[[1]]
mod_index <- as.numeric(mod_parts[1])
mod_value <- mod_parts[2]

# Parse remove index
rem_index <- as.numeric(remove_index)

# TODO: Write your code below
# 1. Create a list with the three initial values (val1, val2, val3)
# 2. Print the original list
# 3. Modify the element at mod_index with mod_value
# 4. Print the list after modification
# 5. Add a new fourth element with value 999
# 6. Print the list after adding
# 7. Remove the element at rem_index (use NULL)
# 8. Print the final list
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals