1

There is a problem I have come across on several cases and have yet to find an elegant solution. I want to be able to select an object (of any type) using its name as a string. The example below shows that I want to access the three pre-defined lists by iterating through the last character of their names, to perform some function-in this case extract the second element of the list and append it to a new list.

my_list_1 = [1,2,3]
my_list_2 = [4,5,6]
my_list_3 = [7,8,9]
my_result = []

for i in range (1,4):
    my_str = 'my_list_'+str (i)
    my_object = object(my_str)
    x = my_object[1]
    my_result.append(x)

print my_result

This code of course doesn't work, because the line " my_object = object(my_str)" can't figure out a way to identify the list with the use of its name as a string.

Has anyone managed to overcome a similar problem?

Thanks

konnidan
  • 15
  • 2
  • Attached code is not correct Python3 code. Please, edit the title. –  Apr 21 '20 at 10:37
  • 2
    Does this answer your question? [How to get the value of a variable given its name in a string?](https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string) – MatsLindh Apr 21 '20 at 10:39
  • You are making stuff more complicated than they are. Just iterate the lists instead of "creating" their names: `for my_object in (my_list1, my_list2, my_list3):`. Or better yet, just create from the start: `lists = [[1,2,3], [4,5,6], [7,8,9]]` and iterate on that. If you get to a point where you need to do what you are trying to, it is a sign of bad design – Tomerikoo Apr 21 '20 at 10:44
  • not sure what you mean NChechulin, I only use Python 3 – konnidan Apr 22 '20 at 09:53

4 Answers4

2

You can use the locals() built-in to access the current local symbol table, returned as a dict. So:

my_list_1 = [1,2,3]
my_list_2 = [4,5,6]
my_list_3 = [7,8,9]
my_result = []

L = locals()
for i in range (1,4):
    my_str = 'my_list_' + str(i)
    my_result.append(L[my_str][1])

print my_result
pallgeuer
  • 1,216
  • 1
  • 7
  • 17
1

Use eval function to treat it as a defined variable :

for i in range (1,4):
    my_str = eval('my_list_' + str(i))    # Here
    my_object = object(my_str)
    x = my_object[1]
    my_result.append(x)

print my_result
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8
1

You can use the values returned by globals() and locals() methods:

my_list_1 = [1,2,3]
my_list_2 = [4,5,6]
my_list_3 = [7,8,9]
my_result = []

for i in range (1,4):
    my_str = 'my_list_'+str (i) 
    my_object = globals()[my_str]
    x = my_object[1]
    my_result.append(x)

print(my_result)
Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
0

simply use eval and not object.

The eval() method parses the expression passed to this method and runs python expression (code) within the program.

In simple terms, the eval() method runs the python code (which is passed as an argument) within the program.

The syntax of eval() is:

eval(expression, globals=None, locals=None)

my_list_1 = [1,2,3]
my_list_2 = [4,5,6]
my_list_3 = [7,8,9]
my_result = []

for i in range (1,4):
    my_str = eval('my_list_'+str (i))
    my_object = my_str
    x = my_object[1]
    my_result.append(x)

print(my_result)

>>> [2, 5, 8]
johnashu
  • 2,167
  • 4
  • 19
  • 44