-2

My code required a (possible nested) list as variable. However, using sys.argv to input a command line argument reads the input as a string.

So given the example input [[1,[2],3,4],5], what easy ways are there to convert this input from str to a list?

Thanks on forehand.

Kibouo
  • 31
  • 6

1 Answers1

0

You can use ast.literal_eval. Though eval should do the same, but it has some consequences in general and should be avoided. See more from here

>>> from ast import literal_eval
>>> literal_eval('[[1,[2],3,4],5]')
[[1, [2], 3, 4], 5]
Community
  • 1
  • 1
Ahsanul Haque
  • 9,145
  • 2
  • 27
  • 48