-1

I was wondering how you could convert the following list such that the strings contained in the list are properly formatted.

["'34872'", "'Johnathan'", "'Bonzo'", "'100'"]

Ideal output:

["34872", "Johnathan", "Bonzo", "100"]
  • 1
    Duplicate of [Remove quotes from String in Python](https://stackoverflow.com/questions/40950791/remove-quotes-from-string-in-python) – esqew Feb 09 '22 at 20:46
  • Start by filing a bug report with whoever produced that list :) – chepner Feb 09 '22 at 20:47

1 Answers1

2

You can use ast.literal_eval in a list comprehension

>>> from ast import literal_eval
>>> data = ["'34872'", "'Johnathan'", "'Bonzo'", "'100'"]
>>> [literal_eval(i) for i in data]
['34872', 'Johnathan', 'Bonzo', '100']

Otherwise you can use simple string replacement

>>> [i.replace("'", "") for i in data]
['34872', 'Johnathan', 'Bonzo', '100']
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218