0

This may have been answered before but it's hard to look up precisely. I have a list of strings where the strings have lists in them (or they would be lists if they were not in strings). I want to return a list of lists.

m,x,y,z may or may not have been defined already

Here is what I might start with:

old_list = ['[1,2,m]','[4.1,3.5,5]','[x,y,z]', '["t","u","v"]']

Here is what I would want (depending on whether m,x,y,z have been defined):

new_list = [[1,2,'m'],[4.1,3.5,5],['x','y','z'],['t','u','v']]
new_list2 = [[1,2,m],[4.1,3.5,5],[x,y,z],['t','u','v']]
  • 1
    Why do you have a list of list literals in the first place? – chepner Jun 08 '19 at 19:21
  • I guess you mean `list = ['[1,2,3]','[4.1,3.5,5]','[x,y,z]', "['t','u','v']"]`, otherwise your last string would break. Also, people, PLEASE stop using `list` as a variable name, it is a python keyword. – Finomnis Jun 08 '19 at 19:23
  • Are you assuming that `x` `y` and `z` already exist? – Patrick Haugh Jun 08 '19 at 19:28
  • 1
    _x,y,z may or may not be variables that already exist_ If they don't exist, `[x,y,z]` has no meaning... – John Gordon Jun 08 '19 at 19:30
  • More answers on similar problem [here](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) – Sheldore Jun 08 '19 at 19:37
  • The case with only strings was answered here https://stackoverflow.com/questions/56510079/return-list-of-list-from-list-of-strings-that-have-lists-in-them?noredirect=1#comment99607560_56510079 –  Jun 09 '19 at 00:03

2 Answers2

0

Not sure what your problem is, it works for me...

x = 10
y = 11
z = 10

list_in = ['[1,2,3]','[4.1,3.5,5]','[x,y,z]', "['t','u','v']"]

list_out = [eval(elem) for elem in list_in]

print(list_out)

Output:

[[1, 2, 3], [4.1, 3.5, 5], [10, 11, 10], ['t', 'u', 'v']]
Finomnis
  • 18,094
  • 1
  • 20
  • 27
0
import ast

mylist = ['[1,2,3]','[4.1,3.5,5]','[x,y,z]', "['t','u','v']"]
mynewlist = [ast.literal_eval(i) for i in mylist]

Of course this example doesn't work exactly as-is, because you haven't defined x, y or z.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • What's the advantage of ast over just plain eval()? – Finomnis Jun 08 '19 at 19:30
  • `ast.literal_eval()` is safe, and `eval()` isn't. For this toy example both are fine, but presumably the OP's real code is more complicated. – John Gordon Jun 08 '19 at 19:31
  • Aaah, I get it. Had to look it up, didn't understand what 'safe' means in this case. ast does a check of whether or the syntax is correct *before* running it. eval just runs it and errors at some point. – Finomnis Jun 08 '19 at 19:35
  • I get the same error. Looks like ast can't deal with the `[x,y,z]`. – Finomnis Jun 08 '19 at 19:38
  • 1
    `literal_eval()` also limits the input to literal values, as the name implies. If the input maliciously contains a function call to, say, `os.unlink()`, eval will gladly run it, but literal_eval won't. – John Gordon Jun 08 '19 at 19:41
  • So the more generic answer would be: whatever you are doing, stop doing it, there is most certainly a better way :D – Finomnis Jun 08 '19 at 19:45