-3

Below is my list

l =['[37i9dQZF1DX5, 37i9dQZF1DWTR, 37i9dQZF1DX0s5]']

I want to convert it into

l =[37i9dQZF1DX5, 37i9dQZF1DWTR, 37i9dQZF1DX0s5]

I tried using eval but it doesn't work!

Smriti
  • 25
  • 6
  • 3
    You can't have that result, because string should be enclosed in quotes. – Austin Oct 30 '20 at 08:46
  • Yes true, i am stuck with thus.I tried https://stackoverflow.com/questions/41945880/how-do-i-extract-the-list-inside-a-string-in-python but it doesn't work as i dont have a string inside – Smriti Oct 30 '20 at 08:52
  • Look at [str.strip](https://docs.python.org/3/library/stdtypes.html#str.strip) for removing the brackets and [str.split](https://docs.python.org/3/library/stdtypes.html#str.split) for splitting the remaining string into a list. – Wups Oct 30 '20 at 08:58

2 Answers2

0

You ca do this,

l =['[37i9dQZF1DX5, 37i9dQZF1DWTR, 37i9dQZF1DX0s5]']
l2 = l[0].replace("[","").replace("]","").split(", ")

Which will give you the following as the content of l2

['37i9dQZF1DX5', '37i9dQZF1DWTR', '37i9dQZF1DX0s5']
scotty3785
  • 6,763
  • 1
  • 25
  • 35
0

If the contents of the list are strings, you can use json

import json
l = '["37i9dQZF1DX5", "37i9dQZF1DWTR", "37i9dQZF1DX0s5"]'#I added " to make strings
print (json.loads (l))
type (json.loads (l))

The output will be:

["37i9dQZF1DX5", "37i9dQZF1DWTR", "37i9dQZF1DX0s5"]
<class 'list'>
Sagitario
  • 102
  • 8