Convert python list to R list and also a dataframe

Can someone give me the simplest way to convert this list in python

home = {
"A" : '1. OptionA',
"B" : '2. OptionB',
"C" : '3. OptionC',
"D" : '4. OptionD'
}
to an R list plus also an R dataframe

Thanks

Hi @ply,

Just to be specific, the python code you provided creates a dict and not a list. For R, the closest equivalent structure for a python dict object is a named list. If you are using {reticulate}, the dict is automatically converted to a named list in R when access from the python module, and a named listed can be easily converted to a data frame using as.data.frame(). For example

library(reticulate)

py_run_string("name = {
  'A' : '1. OptionA',
  'B' : '2. OptionB',
  'C' : '3. OptionC',
  'D' : '4. OptionD'
  }")

py$name
#> $A
#> [1] "1. OptionA"
#> 
#> $B
#> [1] "2. OptionB"
#> 
#> $C
#> [1] "3. OptionC"
#> 
#> $D
#> [1] "4. OptionD"
typeof(py$name)
#> [1] "list"

as.data.frame(py$name)
#>            A          B          C          D
#> 1 1. OptionA 2. OptionB 3. OptionC 4. OptionD

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.