0
0
R Programmingprogramming~10 mins

Nested lists in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested lists
Create outer list
Add inner lists as elements
Access elements by outer index
Access inner elements by inner index
Use nested loops or indexing to read/change values
This flow shows how an outer list contains inner lists, and how to access elements step-by-step.
Execution Sample
R Programming
outer_list <- list(c(1,2), c(3,4,5))
print(outer_list[[1]][2])
outer_list[[2]][3] <- 10
print(outer_list)
Create a nested list, access an inner element, modify an inner element, then print the whole list.
Execution Table
StepActionExpressionResultNotes
1Create outer list with two inner vectorsouter_list <- list(c(1,2), c(3,4,5))outer_list = list(c(1,2), c(3,4,5))Outer list has 2 elements, each is a vector
2Access second element of first inner vectorouter_list[[1]][2]2Access inner element by double bracket then index
3Modify third element of second inner vectorouter_list[[2]][3] <- 10outer_list[[2]] = c(3,4,10)Change value 5 to 10
4Print modified outer listprint(outer_list)list(c(1,2), c(3,4,10))Shows updated nested list
💡 All steps complete, nested list created, accessed, modified, and printed.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
outer_listNULLlist(c(1,2), c(3,4,5))list(c(1,2), c(3,4,10))list(c(1,2), c(3,4,10))
Key Moments - 2 Insights
Why do we use double brackets [[ ]] to access inner lists instead of single brackets [ ]?
Double brackets [[ ]] extract the element itself (the inner vector), while single brackets [ ] return a sublist. See step 2 in execution_table where outer_list[[1]] gives the vector c(1,2), allowing indexing inside.
How does modifying outer_list[[2]][3] change the nested list?
Modifying outer_list[[2]][3] changes the third element of the second inner vector directly. Step 3 shows the value 5 replaced by 10 inside the nested list.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what value is accessed by outer_list[[1]][2]?
A2
B1
C3
D4
💡 Hint
Check the 'Result' column in step 2 of execution_table.
At which step does the nested list get modified?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for the step where outer_list[[2]][3] is assigned a new value.
If we used single brackets [ ] instead of double brackets [[ ]] to access inner lists, what would happen?
AWe get the inner vector directly
BIt causes an error
CWe get a sublist containing the inner vector
DIt modifies the outer list structure
💡 Hint
Recall the difference between [ ] and [[ ]] in list indexing in R.
Concept Snapshot
Nested lists in R hold lists inside lists.
Use [[ ]] to get inner elements directly.
Use [ ] to get sublists.
Access inner elements by outer then inner indices.
Modify inner elements by combining [[ ]] and [ ].
Full Transcript
This example shows how to create a nested list in R with two inner vectors. We access an inner element by using double brackets to get the inner vector, then single brackets to get the element inside it. We modify an inner element by assigning a new value using the same indexing. Finally, we print the updated nested list to see the change.