In order to use Python style list comprehensions with enumerations, such as enumerated lists, one way is to install List-comprehension package LC
(developed 2018) and itertools package (developed 2015).
List comprehensions in R
You can find the LC
package here.
install.packages("devtools")
devtools::install_github("mailund/lc")
Example
> library(itertools); library(lc)
> lc(paste(x$index, x$value), x=as.list(enumerate(rnorm(5))), )
[[1]]
[1] "1 -0.715651978438808"
[[2]]
[1] "2 -1.35430822605807"
[[3]]
[1] "3 -0.162872340884235"
[[4]]
[1] "4 1.42909760816254"
[[5]]
[1] "5 -0.880755983937781"
where the programming syntax is not yet as clean and polished as in Python but functionally working and its help outlines:
"The syntax is as follows: lc(expr, lists, predicates) where expr is some expression to be evaluated for all elements in the lists, where
lists are one or more named lists, where these are specified by a name
and an expression name = list_expr, and where predicates are
expressions that should evaluated to a boolean value. For example, to
get a list of all even numbers, squared, from a list x we can write
lc(x ** 2, x = x, x %% 2 == 0). The result of a call to lc is a list
constructed from the expressions in expr, for all elements in the
input lists where the predicates evaluate to true."
where notice that you can leave the predicates empty for example in the above example.
Python-style itertools and enumerations
You can use R's itertools that is very similar to Python's itertools, further in Cran here
library(itertools)
where described
"Various tools for creating iterators, many patterned after functions in the Python itertools module, and others patterned after functions
in the 'snow' package."
Example. enumeration
> for (a in as.list(enumerate(rnorm(5)))) { print(paste(a$index, "index:", a$value))}
[1] "1 index: 1.63314811372568"
[1] "2 index: -0.983865948988314"
[1] "3 index: -1.27096072277818"
[1] "4 index: 0.313193212706331"
[1] "5 index: 1.25226639725357"
Example. enumeration with ZIP
> for (h in as.list(izip(a=1:5, b=letters[1:5]))) { print(paste(h$a, "index:", h$b))}
[1] "1 index: a"
[1] "2 index: b"
[1] "3 index: c"
[1] "4 index: d"
[1] "5 index: e"