1

I am new to python and I have search a lot about this issue. I know there is a way of converting tuples to a list, but somehow it doesn't work for me. Here is my issue.

Say I have:

l_1 = ['a','b','c']
l_2 = ['e','f','g']
l_3 = ['g','h','i']

Then say I have another list of:

l_all = ['l_1','l_2','l_3']

How can I convert it(l_all) to a list of

[['a','b','c'],['e','f','g'],['g','h','i']]

I tried ast package using ast.literal_eval, but I received this error:

ValueError: malformed node or string: <_ast.Name object at 0x00000172CA3C5278>

I also tried to use json package, still no luck.

I tried just output ast.literal_eval('l_1'), not working either.

I'd really appreciate if anyone can help on this.

Thanks a lot!

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • 5
    How did you get into this situation? – Stephen Rauch Mar 17 '18 at 17:07
  • 1
    Are you asking for `ls_all = [ls_1, ls_2, ls_3]`? – quantik Mar 17 '18 at 17:09
  • I don't see any tuples, what is the relation to your problem? – Ulrich Eckhardt Mar 17 '18 at 17:09
  • 1
    BTW you have a typo `1_1` => `l_1`, but you got it right afterwards, so I took the liberty of fixing it. – Jean-François Fabre Mar 17 '18 at 17:13
  • @cricket_007 actually it's the opposite. OP doesn't want to create variable, OP wants to _evaluate_ variables. – Jean-François Fabre Mar 17 '18 at 17:20
  • @StephenRauch apparently they have like more then 50 lists( like l_1 to l_50) and each list has different values. Technically I can just do [l_1, l_2,....l_50] but it looks ugly. So I used a loop to get ['l_1', 'l_2' ....'l_50'], but now its all strings not list object. yeah thats how I got myself into this lol – kingslucifer Mar 17 '18 at 17:44
  • @kingslucifer, As you now know naming your variables `l_1`, `l_2` ... is not a good idea in Python. Use a list or a dict instead. – Stephen Rauch Mar 17 '18 at 17:45
  • @StephenRauch ohhh thanks for the reminder. I didn't name them as this haha. Just using it as example :) – kingslucifer Mar 17 '18 at 17:48
  • >>> l_1 = ['a','b','c'] >>> l_2 = ['e','f','g'] >>> l_3 = ['g','h','i'] >>> l_all = ['l_1','l_2','l_3'] >>> [eval(i) for i in l_all] [['a', 'b', 'c'], ['e', 'f', 'g'], ['g', 'h', 'i']] –  Mar 18 '18 at 00:05
  • @Jean-FrançoisFabre I am not sure I would agree with *irrelevant*, but it may not be perfect, please feel free to reopen it – Stephen Rauch Mar 18 '18 at 17:05
  • never mind. The question title sucks (and the question is not very good as well) but some of the answers address the `globals()` function, and this is solved anyway. I'm not going to pretend that this question is super-original. Leave it be :) – Jean-François Fabre Mar 18 '18 at 17:14

6 Answers6

3

That sounds like a problem that should be fixed upstream

ast.literal_eval evaluates literals. eval is just 1) cheating and 2) so dangerous I wouldn't recommend it at all.

Anyway, you could scan global then local variables using global & local dicts in a list comprehension:

l_1 = ['a','b','c']
l_2 = ['e','f','g']
l_3 = ['g','h','i']
l_all = ['l_1','l_2','l_3']

l_all = [globals().get(x,locals().get(x)) for x in l_all]

result:

[['a', 'b', 'c'], ['e', 'f', 'g'], ['g', 'h', 'i']]

globals().get(x,locals().get(x)) is a quick & dirty code to first look in global vars & fallback to local vars if not found. It could be overcomplicated for your needs.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

You can use a dictionary to store the list names and each associated list:

d = {'l_2': ['e', 'f', 'g'], 'l_3': ['g', 'h', 'i'], 'l_1': ['a', 'b', 'c']}
l_all = ['l_1','l_2','l_3']
final_results = [d[i] for i in l_all]

Output:

[['a', 'b', 'c'], ['e', 'f', 'g'], ['g', 'h', 'i']]

However, to actually access the lists via variable name, you would have to use globals:

l_1 = ['a','b','c']
l_2 = ['e','f','g']
l_3 = ['g','h','i']
l_all = ['l_1','l_2','l_3']
new_l = [globals()[i] for i in l_all]

Output:

[['a', 'b', 'c'], ['e', 'f', 'g'], ['g', 'h', 'i']]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

Then say I have another list of:

l_all = ['l_1','l_2','l_3']

Let's say you don't create this using strings, and use the list variables directly

You can then get the wanted output

l_all = [l_1,l_2, l_3]
Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • oh, cuz i have a lot of those lists and those are defined by users. I want to keep them more organized so I used a loop to create l_all. – kingslucifer Mar 17 '18 at 17:36
  • Okay, so something like `l_all = [user.get_list() for user in users]` would not be possible? – OneCricketeer Mar 17 '18 at 17:37
  • My point here is that "numbered variables" are a sign that you need a better design. For example, you can use a SQLite database for storing user input rather than just an in-memory list. Then you can query all rows of the database to get a list of all lists – OneCricketeer Mar 17 '18 at 17:39
  • yeah i totally agree on this. I know its dumb...this project is for my company and theres data control sigh – kingslucifer Mar 17 '18 at 17:49
1

You can simply do:

l_1 = ['a','b','c']
l_2 = ['e','f','g']
l_3 = ['g','h','i']
l_all = ['l_1','l_2','l_3']

print(list(map(lambda x:globals()[x],l_all)))

output:

[['a', 'b', 'c'], ['e', 'f', 'g'], ['g', 'h', 'i']]
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
0

You can use locals()

l_1 = ['a','b','c']
l_2 = ['e','f','g']
l_3 = ['g','h','i']

l_all = ['l_1', 'l_2', 'l_3']

all_local_variables = locals()
l_all_values = [all_local_variables[i] for i in l_all]

And you should be aware that you can get KeyError if no such variable present in current scope, for that you can all_local_variables.get(i), that will return None if not present or set default as all_local_variables.get(i, 'default')

vishes_shell
  • 22,409
  • 6
  • 71
  • 81
0

The simplest way to do this is to evaluate your strings in l_all. Something like this

>>> l_1 = ['a','b','c']
>>> l_2 = ['e','f','g']
>>> l_3 = ['g','h','i']
>>> l_all = ['l_1','l_2','l_3']
>>> [ eval(x) for x in l_all ]

The output is

[['a', 'b', 'c'], ['e', 'f', 'g'], ['g', 'h', 'i']]
rth
  • 2,946
  • 1
  • 22
  • 27
  • 1
    Why the downvote? It is the exact answer to the question! Please leave a comment if you downvote. – rth Mar 17 '18 at 17:26