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

4
  • Attached code is not correct Python3 code. Please, edit the title. Commented Apr 21, 2020 at 10:37
  • 2
    Does this answer your question? How to get the value of a variable given its name in a string? Commented Apr 21, 2020 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 Commented Apr 21, 2020 at 10:44
  • not sure what you mean NChechulin, I only use Python 3 Commented Apr 22, 2020 at 9:53

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Pall, this is most useful
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

Comments

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)

1 Comment

Thanks Susmit, the is also a very simple and elegant solution.
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]

1 Comment

Thanks John, this works perfectly and was quite what I was after. I appreciate the help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.