R lists
last modified November 19, 2026
A list is an ordered collection whose elements can have different types. A vector requires every element to share one type, but a list can hold a number, a string, a logical value, a vector, and another list in the same object. This makes lists the natural container for records, function results, and model objects. This article shows how to build lists, select their elements with the three bracket forms, nest them, change them, and flatten them back into a vector.
Creating a list
The function list builds a list from its arguments, and names on
the left of the equals sign become the element names. Printing a named list
shows one $name heading per element, followed by the value. The
function length counts the elements of the list, not the values
inside them.
student <- list(name = "Ada", grade = 90, passed = TRUE)
print(student)
cat("length:", length(student), "\n")
The list has three elements, each of a different type. The printout is longer than that of a vector because each element is displayed on its own, with its name and its type. A list element is also called a component, and the names are the component names.
$ Rscript building.R $name [1] "Ada" $grade [1] 90 $passed [1] TRUE length: 3
Accessing elements
The dollar sign selects a named element and returns the element itself.
The double bracket selects by name or by position and also returns the element
itself. For a named element the two forms are equivalent, which the next script
confirms with identical.
student <- list(name = "Ada", grade = 90, passed = TRUE)
cat("name:", student$name, "\n")
cat("grade:", student[["grade"]], "\n")
cat("same:", identical(student$grade, student[["grade"]]), "\n")
student$name is the shortest form and reads like a field of a
record. student[["name"]] is the programmatic form, because the
name can be computed and stored in a variable. The position form
student[[1]] returns the same value when the element is first.
$ Rscript access.R name: Ada grade: 90 same: TRUE
Single and double brackets
The single bracket is the source of most list confusion. On a list,
student["name"] does not return the element; it returns a new
list that contains that one element. The double bracket returns the element
inside. The function is.list tests whether a value is a list, and
the next script uses it to show the difference.
student <- list(name = "Ada", grade = 90, passed = TRUE)
one <- student["name"]
value <- student[["name"]]
print(one)
print(value)
cat("one is a list:", is.list(one), "\n")
cat("value is a list:", is.list(value), "\n")
The first printout still has the $name heading, while the second
prints the bare string. The single bracket is useful when a subset of several
elements should stay a list, for example when passing it to a function that
expects a list. The double bracket and the dollar sign are for reaching a
single value.
$ Rscript brackets.R $name [1] "Ada" [1] "Ada" one is a list: TRUE value is a list: FALSE
Nested lists
A list element can be another list, which is how records with sub-records are
represented. The dollar sign chains through the levels, so
student$address$city reads the city from the inner list. A list
element can also be a vector, and functions such as mean then work
on that element directly.
student <- list(
name = "Ada",
scores = c(90, 85, 88),
address = list(city = "London", country = "UK")
)
cat("city:", student$address$city, "\n")
cat("mean score:", mean(student$scores), "\n")
The outer list has three elements: a string, a numeric vector, and an inner
list with two elements. Nesting can continue to any depth, and
str is the quickest way to see the whole structure of a deeply
nested object. A data frame is essentially a named list of vectors, which is
why the same bracket rules reappear in that article.
$ Rscript nested.R city: London mean score: 87.66667
Adding and removing elements
Assignment with a new name adds an element, and assignment with an existing
name replaces it. Assigning NULL removes the element entirely,
because NULL is the absence of a value rather than a missing
value. The function str shows the structure after the changes.
student <- list(name = "Ada", grade = 90) student$passed <- TRUE student$grade <- 95 student$grade <- NULL str(student)
The final list has two elements: the original name and the added
passed flag. The grade was replaced and then removed, so it does
not appear. Removing an element shortens the list and shifts the positions of
the later elements, which matters when the list is accessed by number.
$ Rscript editing.R List of 2 $ name : chr "Ada" $ passed: logi TRUE
Flattening a list
unlist turns a list into one atomic vector by concatenating its
elements. Names are composed from the component name and the position inside
the component, so a with three values becomes
a1, a2, and a3. If the components have
different types, the result is coerced to the common type.
parts <- list(a = 1:3, b = 4:5, c = 6)
flat <- unlist(parts)
print(flat)
cat("length:", length(flat), "\n")
cat("names:", names(flat), "\n")
The six values become a numeric vector with six names. Flattening is convenient
for simple summaries, but it loses the grouping that the list provided, so it
is not a substitute for keeping structured data structured. Use
unlist(parts, use.names = FALSE) when only the values matter.
$ Rscript flatten.R a1 a2 a3 b1 b2 c 1 2 3 4 5 6 length: 6 names: a1 a2 a3 b1 b2 c
A complete example
The next script stores three student records in a list and finds the best
score. Each record is itself a list with a name and a score. The loop visits
one record at a time, and the variable student holds a whole list
on every pass.
students <- list(
list(name = "Ada", score = 90),
list(name = "Grace", score = 85),
list(name = "Alan", score = 66)
)
best <- students[[1]]
for (student in students) {
if (student$score > best$score) best <- student
}
cat("records:", length(students), "\n")
cat("best:", best$name, "at", best$score, "\n")
students[[1]] selects the first record, not a list of one record,
so best$score reads the score directly. The comparison replaces
best whenever a higher score appears, and the final line prints
the winner. The same pattern scales to hundreds of records and is the reason
lists are the usual return type for functions that produce several related
values.
$ Rscript records.R records: 3 best: Ada at 90
Next steps
The matrices article adds two dimensions to the atomic vector, and the data frames article later in the series builds a table from a named list of equal-length vectors. The apply family shows how to call a function for every element of a list without writing the loop, and the object-oriented articles use lists as the basis of S3 objects.
Source
The R Language Definition: lists
R documentation: extract or replace parts of an object
In this article we have built lists with named components, selected them with
$, [[, and [, and seen why the single
bracket returns a list while the double bracket returns the element. We nested
lists, added and removed components, flattened a list with unlist,
and searched a list of records. The series continues with
R matrices.
Author
List all R Language tutorials.