1

I've loaded "reticulate" package to run python code in Rstudio. Below the code:

# Initialize an empty dictionary
D = {} 

L = [['a', 1], ['b', 2], ['a', 3], ['c', 4]]

I get the following error:

Error: unexpected '[[' in "L = [["

Why did this happen?

No answers on Google helped me.

Phil
  • 7,287
  • 3
  • 36
  • 66
Fra_Silver
  • 11
  • 2
  • What you've shown in your question is **valid** Python. If *reticulate* (whatever that is) thinks it's wrong then maybe someone interested in *reticulate* can help. It's not a Python issue – DarkKnight May 30 '23 at 15:57
  • That's not a Python error message. I don't know anything about the other tools you mentioned though. – wjandrea May 30 '23 at 15:58
  • 1
    Probably RStudio is running this is as if it were R code, not Python. Does your file have a `.py` extension? – slothrop May 30 '23 at 16:00
  • 1
    Or, more broadly: are you using one of the methods described here to run Python code? https://rstudio.github.io/reticulate/ – slothrop May 30 '23 at 16:06
  • As slothrop suggested, basically I hadn't saved file with .py extension. – Fra_Silver May 31 '23 at 06:01

1 Answers1

0

I guess you can try

py_run_string("L = [['a', 1], ['b', 2], ['a', 3], ['c', 4]]")

such that

> L
[[1]]
[[1]][[1]]
[1] "a"

[[1]][[2]]
[1] 1


[[2]]
[[2]][[1]]
[1] "b"

[[2]][[2]]
[1] 2


[[3]]
[[3]][[1]]
[1] "a"

[[3]][[2]]
[1] 3


[[4]]
[[4]][[1]]
[1] "c"

[[4]][[2]]
[1] 4
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81