-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?

2

1 Answer 1

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].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.