Concept Flow - Named list elements
Create list with elements
Assign names to elements
Access elements by name
Use named elements in code
This flow shows creating a list, naming its elements, and then accessing those elements by their names.
my_list <- list(a = 10, b = 20, c = 30) print(my_list) print(my_list$a) print(my_list[["b"]])
| Step | Action | List State | Access Method | Output |
|---|---|---|---|---|
| 1 | Create list with named elements | list(a=10, b=20, c=30) | N/A | N/A |
| 2 | Print entire list | list(a=10, b=20, c=30) | print(my_list) | $a [1] 10 $b [1] 20 $c [1] 30 |
| 3 | Access element 'a' by $ | list(a=10, b=20, c=30) | my_list$a | 10 |
| 4 | Access element 'b' by [["b"]] | list(a=10, b=20, c=30) | my_list[["b"]] | 20 |
| 5 | Access element 'c' by [[3]] (index) | list(a=10, b=20, c=30) | my_list[[3]] | 30 |
| 6 | Access non-existent element 'd' | list(a=10, b=20, c=30) | my_list$d | NULL |
| Variable | Start | After Creation | Final |
|---|---|---|---|
| my_list | NULL | list(a=10, b=20, c=30) | list(a=10, b=20, c=30) |
Named list elements in R: - Create with list(name=value, ...) - Access by name using $ or [["name"]] - Access by position using [[index]] - Non-existent names return NULL, no error - Useful for clear, readable code