0

I am facing a small problem in python which I am not able to solve quickly.

How to convert a = ['(100,9),(200,10)'] (list of tupled strings) to a = [(100,9),(200,10)] (list of tuples)

I tried:

- [tuple(word) for word in a]
- a.strip("'")
- a.replace("'", " ")

None of above mentioned solutions are working for me.

Radheya
  • 779
  • 1
  • 11
  • 41
  • Substitute *"tuple"* for *"dictionary"* when reading... Your string will become a tuple of tuples, but I'm sure you can figure out how to flatten it to whatever you need. – jonrsharpe Nov 18 '15 at 14:32
  • @jonrsharpe Sorry for duplicate. Thank you for making it clear and the source. – Radheya Nov 18 '15 at 14:34

1 Answers1

1

Use literal_eval func from ast module.

>>> from ast import literal_eval
>>> a = ['(100,9),(200,10)']
>>> list(literal_eval(a[0]))
[(100, 9), (200, 10)]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274