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?
Asked
Active
Viewed 142 times
-1

Lorenzo Beronilla
- 45
- 11
-
1One way might be https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user – Devesh Kumar Singh Apr 09 '20 at 03:46
-
@DeveshKumarSingh Thanks for the tip. However, the input is still a set of integers and not a list. – Lorenzo Beronilla Apr 09 '20 at 03:47
1 Answers
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