Challenge - 5 Problems
Named List Elements Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing named list elements
What is the output of the following R code?
R Programming
my_list <- list(a = 10, b = 20, c = 30) result <- my_list$b print(result)
Attempts:
2 left
💡 Hint
Remember that named list elements can be accessed by their names using the $ operator.
✗ Incorrect
The list has elements named 'a', 'b', and 'c' with values 10, 20, and 30 respectively. Accessing my_list$b returns the value 20.
❓ Predict Output
intermediate2:00remaining
Output of modifying named list elements
What will be the output after running this R code?
R Programming
my_list <- list(x = 5, y = 15) my_list$y <- my_list$y + 10 print(my_list$y)
Attempts:
2 left
💡 Hint
Think about how the value of 'y' is updated by adding 10.
✗ Incorrect
Initially, my_list$y is 15. Adding 10 updates it to 25, so printing my_list$y outputs 25.
🔧 Debug
advanced2:00remaining
Identify the error in accessing named list elements
What error does this R code produce?
R Programming
my_list <- list(a = 1, b = 2) print(my_list[['c']])
Attempts:
2 left
💡 Hint
Accessing a non-existent name with double brackets produces an error.
✗ Incorrect
Using double brackets with a non-existent name produces an error: subscript out of bounds.
❓ Predict Output
advanced2:00remaining
Output of named list element extraction with double brackets
What is the output of this R code?
R Programming
my_list <- list(a = 100, b = 200) result <- my_list[['a']] print(result)
Attempts:
2 left
💡 Hint
Double brackets extract the element itself, not a sublist.
✗ Incorrect
Using double brackets with a name extracts the element value directly, so my_list[['a']] returns 100.
🧠 Conceptual
expert3:00remaining
Number of elements in a named list after modification
Consider the following R code. How many elements does my_list have after execution?
R Programming
my_list <- list(one = 1, two = 2) my_list$three <- 3 my_list$two <- NULL
Attempts:
2 left
💡 Hint
Assigning NULL to a list element removes it.
✗ Incorrect
Initially, my_list has 2 elements. Adding 'three' makes 3 elements. Setting 'two' to NULL removes it, so 2 elements remain.