# [SOLVED] How can I efficiently loop trough a list containing vectors of different length

## Issue

I have a list containing vectors of different length which I need to loop trough efficently. As an example here I want to use the print function on each element. However as I will encounter this problem repeatedly, I would like to find an efficient way to do so.

``````list <- list(1, c(1:4), c(3:10))

for (i in seq_along(list)) {
for(j in seq_along(list[[i]])){
print(list[[i]][j])
}
}
``````

Any help is appreciated.

## Solution

Because you are interested in efficiency, I’ve added a benchmarking example to evaluate (1) your approach (2) the approach propose by @Sotos (3) an approach iterating through the list elements (vectors) using lapply and (4) an approach iterating through the list elements (elements of vectors) using lapply and sapply:

``````list2examine <- list(1, c(1:4), c(3:10))

benchmark("original"= {
for (i in seq_along(list2examine)) {
for(j in seq_along(list2examine[[i]])){
print(list2examine[[i]][j])
}
}
}, "Sotos" = {rapply(list2examine, print)},
"lapply" = {lapply(list2examine, function(x) {print(x)} )},
"lapplySapply" = {lapply(list2examine, function(x) { sapply(x, function(i) {print(i)} ) })},
"ismirsehregal" = { print(unlist(list2examine))},

replications=1000,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self"))

test replications elapsed relative user.self sys.self
5 ismirsehregal         1000    0.08    1.000      0.07     0.02
3        lapply         1000    0.11    1.375      0.11     0.00
4  lapplySapply         1000    0.34    4.250      0.36     0.00
1      original         1000    2.80   35.000      2.81     0.07
2         Sotos         1000    0.14    1.750      0.17     0.00
``````

As you can see your approach is the slowest ("elapsed") which you’ve already expected. lapplySapply is slower than rapply, I’ll guess that has to do something with the fact that rapply is a recursive function. If you want to read further on benchmarking or how to interpret the result of the function I recommend the article https://www.r-bloggers.com/2017/05/5-ways-to-measure-running-time-of-r-code/.

Note that I’ve changed the name of your list because variable name should not be the same as built-in functions of r.

Also, have in mind that however, these functions are all printing the entry in the list, they differ in the form of iteration.