0

I have a the following string in Python:

x = "['good', 'bad', 'something', 'another']"

And I want to convert it into a list y, such that y[0] would give me good, and y[1] would give me bad.

I tried many ways such as creating a for loop and trying x[0] or x[0][0], but all I reached was the first char, not the whole word. I also tried:

y = list(x)

But no help.

Is there a way to do it on Python?

Madno
  • 910
  • 2
  • 12
  • 27
  • 2
    Use the ast module...`ast.literal_eval(x)` – Rakesh May 28 '18 at 10:24
  • @Rakesh Any way to do it without extra modules? – Madno May 28 '18 at 10:25
  • You can use `eval` without extra modules, but as a rule, this is less safe. – hilberts_drinking_problem May 28 '18 at 10:25
  • What you are looking for is called **a parser**. In order to get one you need to know the rules governing the underlying string. Where did you get it from? Is that a syntax for Python lists? In that case you can use the built-in `ast` module. Without more info all answers are just shots in the dark. Because, you know, `y = ['good', 'bad', 'something', 'another']` is a proper answer as well. And how are we supposed to know what is the general form of that string? – freakish May 28 '18 at 10:28
  • @freakish I don't think any of this is relevant. Other people were able to provide answers and there's no more info to provide. I just have the string and would like to convert it to a list. – Madno May 28 '18 at 10:34
  • @Madno You don't find this relevant? And there is no more info? Amusing, so let me give you my answer: `y = ['good', 'bad', 'something', 'another']`. You had a string, now you have a list. Faster and easier to understand then any other. – freakish May 28 '18 at 10:36
  • @Rakesh Good, but I don't like your answer, which is why I won't accept it. – Madno May 28 '18 at 10:41
  • 1
    `ast` is a built in module. Why wouldn't you use it? – Daniel Roseman May 28 '18 at 10:59
  • @DanielRoseman I just wanted a Pythonic way of doing the job, without need to import anything for a line or two (because I won't use ast anywhere else in my program). – Madno May 28 '18 at 11:01
  • 1
    `ast.literal_eval` is the Pythonic way of safely parsing something generated with e.g. `repr`. Certainly not generating a bunch of intermediate strings that don't care if your input is structurally relevant. – Yann Vernier May 28 '18 at 11:04
  • 1
    That *is* the Pythonic way. It's a part of the standard library - how much more Pythonic can you get? It's already installed, it doesn't matter if you don't use it anywhere else. – Daniel Roseman May 28 '18 at 11:04
  • 1
    I wonder where this idea that avoiding the *standard* library is Pythonic comes from. – Yann Vernier May 28 '18 at 11:14

4 Answers4

3

use ast module

>>> import ast
>>> y = ast.literal_eval(x)
['good', 'bad', 'something', 'another']
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
  • Should perhaps note that this applies for Python literals; depending on string contents, it might produce other types than a list, but it won't have arbitrary side effects. – Yann Vernier May 28 '18 at 11:18
1

Without using any modules. Use str.strip to remove brackets and then use str.split

Ex:

x = "['good', 'bad', 'something', 'another']"
x = map(str.strip, x.strip("[]").replace("'", "").split(","))

Output:

['good', 'bad', 'something', 'another']
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1
x = eval("['good', 'bad', 'something', 'another']")

The eval command does just that, but please avoid using it if you are not sure of what you are doing.

If, for example, you try to put runnable code inside that string, the eval command will run it, potentially disrupting your system or opening it to external attacks. I assumed that you were working on a toy script that doesn't involve networking or access from external sources

HitLuca
  • 1,040
  • 9
  • 33
  • 3
    Using `eval` is a bad practice, https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice – akash karothiya May 28 '18 at 10:29
  • 2
    `'eval(compile("import sys;print(\'This is my program now!\');sys.exit(1)", "", "exec"))'` works great with this approach. – Yann Vernier May 28 '18 at 11:15
  • @HitLuca you might be ok to let any script kiddie take control of your system, but most of us would rather avoid such issues. Would you be kind enough to either edit your post to explain why one should NEVER use this solution, or just plain remove your post ? – bruno desthuilliers May 28 '18 at 11:20
0

You can use string build in function

 x = "['good', 'bad', 'something', 'another']"  
 y = x.replace("\"", "").replace("[", "").replace("]","").replace("'", "").split(',')

You will get result:
y[0] as good

Deval
  • 126
  • 1
  • 11