-1

How might I be able to input [1,2,3] into python and store it as a list and not as a string (i.e.'[1,2,3]'). input() returns a string so does anybody have any ideas?

1 Answers1

4

It sounds like you want to evaluate the string as a Python expression. But don't use eval, which is unsafe. Instead use literal_eval from ast:

import ast
r = ast.literal_eval("[1, 2, 3]")

This gives r the value [1, 2, 3].

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41