Access Index Names of List Using lapply Function in R
Last Updated :
23 Jul, 2025
The lapply() method in R programming language returns a list of the same length as that of the supplied vector, each element of which is obtained as the result of applying FUN to the corresponding element of the vector.
Syntax:
lapply (X, FUN, ...)
Parameter :
X - an atomic vector or list to apply the function on
FUN - the function to be applied over each element of X.
Method 1: Using seq_along()
The seq_along() method in R is used to generate a sequence of elements of a length equivalent to that of the input length of the passed argument. The generated sequence always begins with the integer values starting from 1.
Syntax:
seq_along (X)
Parameter :
X - passed argument
The seq_along() generated sequence is passed as the first argument of the function lapply(). The function FUN comprises the concatenation of the names assigned to different components of the list which can be accessed using the index and the list name along with the component vectors or list values appended together using the comma separator. The index names of the specified list can be accessed using the following syntax :
names (list-obj)[[index-cntr]]
The element values can be accessed using the following syntax :
list-obj [[index-cntr]]
Example:
R
# declaring and creating a list
lst <- list(ele1 = 1:5,
ele2 = c(FALSE,TRUE),
ele3 = LETTERS[4:9])
# printing list contents
print ("List contents : ")
print (lst)
# accessing elements
print ("Accessing elements : ")
lapply(seq_along(lst),
function(i) paste(names(lst)[[i]],
"->",
paste(lst[[i]], collapse = ", ")))
Output
[1] "List contents : "
$ele1
[1] 1 2 3 4 5
$ele2
[1] FALSE TRUE
$ele3
[1] "D" "E" "F" "G" "H" "I"
[1] "Accessing elements : "
[[1]]
[1] "ele1 -> 1, 2, 3, 4, 5"
[[2]]
[1] "ele2 -> FALSE, TRUE"
[[3]]
[1] "ele3 -> D, E, F, G, H, I"
Method 2: Using seq()
The seq() method can also be used to traverse through the list and its components individually. The method works similarly to the seq_along() method.
seq() function in R is used to create a sequence of elements in a Vector. It takes the length and difference between values as an optional argument.
Syntax:
seq(from, to, by, length.out)
Parameters:
from: Starting element of the sequence
to: Ending element of the sequence
by: Difference between the elements
length.out: Maximum length of the vector
Example:
R
# declaring and creating a list
lst <- list(ele1 = 1:5,
ele2 = c(FALSE,TRUE),
ele3 = LETTERS[4:9])
# printing list contents
print ("List contents : ")
print (lst)
# accessing elements
print ("Accessing elements : ")
lapply(seq(lst),
function(i) paste(names(lst)[[i]], "->",
paste(lst[[i]], collapse = ", ")))
Output
[1] "List contents : "
$ele1
[1] 1 2 3 4 5
$ele2
[1] FALSE TRUE
$ele3
[1] "D" "E" "F" "G" "H" "I"
[1] "Accessing elements : "
[[1]]
[1] "ele1 -> 1, 2, 3, 4, 5"
[[2]]
[1] "ele2 -> FALSE, TRUE"
[[3]]
[1] "ele3 -> D, E, F, G, H, I"
Similar Reads
Count Number of List Elements in R In this article, we will explore how to count the number of elements in a list in R, including both simple and nested lists. We'll use two key functions:length() to count the number of top-level elements in a list.lengths() to count the number of elements within each top-level list component.These f
2 min read
Convert Values in Column into Row Names of DataFrame in R The row names in R are by default mapped to the row numbers, beginning with the integer value 1. The row names can be modified easily and reassigned to any possible string vector to assign customized names. Since, the row numbers are practically equal in each column of the dataframe, therefore the c
4 min read
Convert Row Names into Column of DataFrame in R In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which
3 min read
How to Set Column Names within the aggregate Function in R In this article we will discuss how to set column names with the aggregate function in R programming language. The aggregate method in base R is used to divide the data frame into smaller subsets and compute a summary statistics for each of the formed groups. The function to be applied can be sum, m
3 min read
How to convert factor levels to list in R ? In this article, we are going to discuss how to convert the factor levels to list data structure in R Programming Language. We can get the levels of the vector using factor() function Syntax: factor(vector) Return type: vector elements with levels. If we want to get only levels, Then we can use leve
2 min read
Create empty DataFrame with only column names in R In this article, we are going to discuss how to create a data frame in r with column names and create an empty data frame with column names in the R Programming Language. The basic syntax for creating a data frame is using data. frame(). Create a Data Frame with Values and column NamesR # Define the
3 min read