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