0

I saved a list to a csv and want to import it again but it's in string format now. Is there a simple way to convert it to a list again?

string = '[(1, 2), (3, 4), (5, 6)]'

L = list(map(int, string))

print(L)

Error:

L = list(map(int, string))
ValueError: invalid literal for int() with base 10: '['
Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132
  • 2
    Does this answer your question? [How to convert string representation of list to a list?](https://stackoverflow.com/questions/1894269/how-to-convert-string-representation-of-list-to-a-list) – jizhihaoSAMA Dec 11 '20 at 12:00

1 Answers1

2

You can do the following:

string = eval(string)

This will evaluate the expression as list of tuples, and from there adjust to your needs.

The reason for the error you get is because map pass each character from string to int, and string[0] is not a valid character that can be converted to an integer value.

David
  • 8,113
  • 2
  • 17
  • 36