How to Make a List in R: A Complete Beginner's Guide
Learning how to make a list in R is one of the most useful early steps in your data science journey. In practice, lists are R's most flexible data structure: they can hold numbers, text, data frames, functions, and even other lists, all inside a single object. This guide walks you through creating lists, naming them, nesting them, accessing their contents, and avoiding the mistakes that trip up most beginners.
What Is a List in R?
A list is an ordered collection of elements, where each element can be any R object of any type or length. Unlike atomic vectors, which require every element to share the same type, a list is heterogeneous — you can mix numbers, characters, and logical values freely.
my_list <- list(42, "hello", TRUE)
str(my_list)
R also calls lists recursive vectors, because a list can contain other lists, which can contain other lists, and so on. This recursive quality is what makes lists so powerful for storing structured results.
List vs. Vector vs. Data Frame
| Structure | Element types | Access syntax |
|---|---|---|
| Atomic vector | All elements the same type | x[1] |
| List | Any type, any length | x[[1]] or x$name |
| Data frame | Columns may differ in type | df$col or df[["col"]] |
A data frame is technically a special kind of list where every element is a vector of equal length. Understanding lists therefore makes data frames much easier to reason about Nothing fancy..
Basic Ways to Create a List in R
1. Using the list() Function
The list() function is the standard way to build a list. Each argument becomes one element.
person <- list(
name = "Amara",
age = 29,
scores = c(88, 92, 79),
is_active = TRUE
)
str(person)
The output shows four elements of four different types — exactly what an atomic vector could never do.
2. Creating an Unnamed List
If you omit names, R assigns integer positions instead.
mixed <- list(1, "two", 3.0, letters[1:3])
length(mixed) # 4
3. Creating an Empty List
There are two common approaches, and they behave differently:
# A truly empty list (length 0)
empty <- list()
# A pre-allocated list of length 5, filled with NULL
placeholder <- vector("list", 5)
length(placeholder) # 5
Use vector("list", n) when you plan to fill elements inside a loop — pre-allocation is significantly faster than repeatedly growing a list It's one of those things that adds up..
4. Converting an Existing Object into a List
The as.list() function turns a vector or data frame row into a list.
as.list(c(a = 1, b = 2))
as.list(mtcars[1, ]) # first row as a named list
Naming and Renaming List Elements
Names make lists self-documenting. You can supply them at creation time or add them afterward Simple, but easy to overlook..
scores <- list(math = 90, science = 85)
names(scores) # "math" "science"
names(scores) <- c("Math", "Science")
scores # print the named list
unname(scores) # strip the names
Tip: Use str() rather than print() when inspecting unfamiliar lists. str() shows the internal structure compactly, while print() can flood your console with nested output.
Creating Nested and Complex Lists
Because lists are recursive, you can nest them to represent hierarchical data such as JSON responses or configuration files Small thing, real impact..
student <- list(
id = 1001,
name = "Ravi",
courses = list(
list(code = "STAT101", grade = "A"),
list(code = "CS102", grade = "B+")
),
contact = list(email = "ravi@example.com", phone = NULL)
)
Accessing deeply nested values requires chained extraction:
student$courses[[2]]$grade # "B+"
For complex structures, a small helper function keeps your code readable:
get_deep <- function(x, ...) {
purrr::pluck(x, ...) # safe, chainable extraction
}
Accessing Elements: [, [[, and $
This distinction causes more confusion than anything else in R list handling Worth keeping that in mind..
[returns a sublist — always a list, even if it has one element.[[returns the element itself, dropping the list wrapper.$is a shortcut for[[using a name.
scores <- list(math = c(90, 95), science = 85)
scores[1] # list of one element
scores[[1]] # c(90, 95) — a plain numeric vector
scores$math # identical to scores[["math"]]
scores[["math"]] # identical to scores$math
class(scores[1]) # "list"
class(scores[[1]]) # "numeric"
A practical rule: use [[ or $ to retrieve a value, and [ to extract a subset of the list. Note that $ performs partial matching on names, so scores$mat would still return the math element — use [[ with exact = TRUE if you want strict matching.
Honestly, this part trips people up more than it should.
Adding, Modifying, and Removing Elements
Appending a Single
Appending a Single Element
If you have an existing list and want to tack on a new component, append() is the idiomatic choice:
scores <- list(math = 90, science = 85)
# Add a new subject at the end
scores <- append(scores, list(english = 88))
scores
#> $math
#> [1] 90
#>
#> $science
#> [1] 85
#>
#> $english
#> [1] 88
append() always returns a list, even when the input is a vector or scalar. When you need to insert at a specific position, use the optional after argument:
scores <- append(scores, list(history = 92), after = 1)
#> $math
#> [1] 90
#>
#> $history
#> [1] 92
#>
#> $science
#> [1] 85
#>
#> $english
#> [1] 88
Adding Multiple Elements at Once
For bulk insertion, c() works well with lists:
scores <- c(scores,
list(physics = 87),
list(chemistry = 91))
scores
If you prefer a functional style, purrr::list_modify() can be handy for adding named elements conditionally:
library(purrr)
scores <- list_modify(scores,
physics = 87,
chemistry = 91)
Modifying Existing Elements
Changing a value is straightforward—just assign to the appropriate extractor:
scores$math <- 93 # using $
scores[["science"]] <- 86 # using [[
# Partial name matching (caution!)
scores$mat <- 94 # matches "math"
The moment you need to be strict about naming, reach for [[ with exact = TRUE:
scores[["math", exact = TRUE]] <- 94
Replacing Elements
If you want to swap out an entire component, you can use modifyList() from base R. This function returns a new list, leaving the original untouched—great for functional pipelines:
library(base) # already loaded, but shown for clarity
new_scores <- modifyList(scores,
math = 95,
english = NA)
new_scores$math
#> [1] 95
Removing Elements
Three common ways to delete items are:
-
Negative indexing – works on position or name:
scores[-which(names(scores) == "science")] -
dplyr::select(-)– tidy‑verse friendly:library(dplyr) scores %>% select(-science) -
remove()– only for objects in the environment, not list elements Most people skip this — try not to..
# Using negative indexing
scores <- scores[!names(scores) %in% c("science", "history")]
Combining Lists
When you have several related lists (e.g., data from multiple sources) and want a single object, c() again proves useful:
list_a <- list(x = 1:3, y = "A")
list_b <- list(z = TRUE, w = 2.5)
combined <- c(list_a, list_b)
combined$x # 1 2 3
combined$z # TRUE
If you need to align names across lists, purrr::transpose() can reshape a list of lists into a named list of vectors:
library(purrr)
list_of_lists <- list(
scores1 = list(math = 90, science = 85),
scores2 = list(math