# How to use for loop to print all the elements of a list in R?

Applying for loop to a vector or a list is no different, we can simply use in the usual manner. For example, if we have a list called List and we want to print all the elements of the list then we can use the code for(i in List){print(i)}, here i refers to the vectors in the List.

## Example

Live Demo

List<-list(A=LETTERS[1:26],B=rnorm(50,2,1),C=rpois(100,5),D=runif(50,2,3),E=rexp(50,3),F=round(runif(50,1,5),0),G=1:100)
List

## Output

$A [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" " " "K" "L" "M" "N" "O" "P" "Q" "R" "S" [20] "T" "U" "V" "W" "X" "Y" "Z"$B
[1] 2.00587546 2.51976609 3.00092888 -0.09322055 1.28160375 2.51372850
[7] 1.85064902 0.94698668 1.80576447 3.48364091 -0.10322959 0.47136545
[13] 0.97745082 2.45442008 -0.02752721 1.37348735 1.16480536 1.05197157
[19] 3.04930529 1.12683011 1.62577717 1.52076774 2.15920995 0.74680050
[25] 1.46072505 1.37513244 0.88536607 1.31463405 1.03968620 0.85739631
[31] 3.40253373 1.78245400 1.50343557 2.68600981 1.86205795 1.96964744
[37] 2.45979354 -0.12997698 1.77268736 3.57977510 1.36887134 0.69578359
[43] -0.50793426 2.64885978 1.13715367 2.05523230 0.38768866 2.41521900
[49] 1.49863198 2.40155248
$C [1] 4 4 4 6 2 4 2 5 6 5 2 2 4 7 3 7 5 4 8 3 13 7 6 9 3 [26] 6 7 5 8 6 4 4 5 6 4 3 5 3 5 4 5 4 5 8 4 1 2 4 10 9 [51] 3 2 7 2 6 3 1 5 7 3 7 6 2 5 8 7 7 3 9 3 6 2 2 8 7 [76] 6 4 9 4 2 5 5 4 1 5 6 5 7 4 5 3 3 6 5 3 4 9 4 5 4$D
[1] 2.637729 2.178234 2.080350 2.644874 2.311054 2.341333 2.258483 2.563977
[9] 2.767899 2.474238 2.135741 2.961423 2.521220 2.795388 2.833300 2.040131
[17] 2.759688 2.686397 2.553281 2.338263 2.671139 2.018047 2.674863 2.638186
[25] 2.544482 2.924050 2.158069 2.083708 2.345494 2.088061 2.490463 2.626651
[33] 2.386876 2.313154 2.574033 2.963886 2.052571 2.701564 2.587120 2.601845
[41] 2.166091 2.236299 2.511430 2.208991 2.695547 2.159855 2.590239 2.295797
[49] 2.490009 2.737554
$E [1] 0.08559215 0.06408238 0.33736840 0.15217964 0.33952192 0.66868500 [7] 0.15749199 0.18382609 0.17370161 0.16791129 0.03359321 0.06486195 [13] 0.46834204 0.40394242 0.09444054 0.12563253 0.49022354 0.15046070 [19] 0.31010928 0.47584177 0.26749308 0.04880669 0.12168066 0.29834013 [25] 0.96801464 0.19411995 0.77013242 0.17136509 0.06901662 0.23933908 [31] 0.08329398 0.35250951 0.66230820 0.27064894 0.07097516 0.46495136 [37] 0.59157419 0.19611632 0.33286816 0.00268394 0.09891855 0.06711248 [43] 1.18669827 0.24432522 0.04117258 0.57971252 0.20483746 0.76755573 [49] 0.16696971 0.63291417$F
[1] 4 2 5 4 3 2 3 1 4 3 4 2 4 2 5 4 3 2 3 4 5 5 2 3 3 4 4 3 2 3 4 5 2 3 3 1 2 3
[39] 4 3 3 4 2 2 2 2 4 4 3 4
\$G
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100

Creating for loop to print all elements of the List −

for(i in List){print(i)}
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" [20] "T" "U" "V" "W" "X" "Y" "Z"
[1] 1.93632043 3.44600787 -0.37548663 0.02820313 1.53779313 2.29151897
[7] 2.36682473 1.90637424 3.68050429 2.46239595 0.98231679 0.47410780
[13] 2.43062966 0.78372325 3.40713179 2.49426716 2.39736506 1.67222535
[19] 1.33836333 2.61950917 2.14913519 1.47833823 1.51407898 -0.32417220
[25] 2.78334255 2.32820928 2.94538788 2.72676405 2.45864898 2.07040982
[31] 2.00540757 2.05990745 1.06872553 1.54015719 1.14550413 1.83053743
[37] 1.42154501 2.17844179 1.32709648 2.07285498 1.14765621 2.42894143
[43] 1.43871705 3.13744849 1.11409932 2.89626827 2.17641368 2.19421652
[49] 4.20638974 2.78136325
[1] 3 6 4 6 4 8 4 4 3 2 5 4 4 9 12 6 3 9 5 3 7 2 1 5 2
[26] 7 2 6 2 4 7 6 7 5 5 6 4 3 4 3 5 3 5 5 6 4 4 2 4 5
[51] 7 5 6 8 4 3 8 5 3 5 3 3 2 4 3 4 7 7 8 6 5 4 4 3 10
[76] 6 6 4 3 7 5 7 8 0 3 10 6 5 3 2 6 5 9 8 4 5 4 1 1 11
[1] 2.579936 2.108718 2.543215 2.724927 2.241931 2.664744 2.361368 2.821100
[9] 2.361290 2.299780 2.370109 2.386343 2.574619 2.827156 2.350101 2.933151
[17] 2.417123 2.877466 2.210880 2.806961 2.482796 2.979370 2.746896 2.723145
[25] 2.856241 2.292476 2.304214 2.651046 2.784232 2.388978 2.816989 2.853598
[33] 2.922713 2.902011 2.885197 2.048372 2.550389 2.920519 2.015043 2.580556
[41] 2.148432 2.752782 2.349046 2.844716 2.834949 2.565343 2.089208 2.158049
[49] 2.114505 2.242406
[1] 1.008710228 0.017209109 0.685317405 0.096104104 0.039369900 0.008391454
[7] 0.214244966 1.027939101 0.417237856 0.324524232 0.089115314 1.239418979
[13] 0.067536473 0.086289779 0.630843649 0.314140953 0.261509886 0.330478231
[19] 0.173395621 0.265189929 0.275786468 0.319045501 0.062751802 0.061652551
[25] 0.049360117 0.586642448 1.756840602 0.032169100 0.088618954 0.072877214
[31] 0.037873759 0.225511642 0.177181099 0.079314802 0.224341124 0.567073696
[37] 0.376656149 0.345046110 0.009404974 0.222288245 0.002931402 0.652846158
[43] 0.324427433 0.641511924 0.551589377 0.083992971 1.140092308 0.072557058
[49] 1.255553886 1.324285102
[1] 3 1 3 5 2 1 2 2 1 3 3 4 2 4 3 5 1 3 5 1 2 1 4 3 3 4 3 4 4 4 3 2 1 3 4 4 4 2
[39] 4 4 5 5 4 4 4 3 1 2 4 3
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100

Updated on: 08-Oct-2020

1K+ Views