-3

Here is my question:
For example, I have four lists here:

 list_a = [1,2,3]
 list_b = [4,5,6]
 list_c = [7,8,9]
 list_d = [7,10,12]

I want to call these lists using loop.
Define:

 var = ["a","b","c","d"]  

If it's in string format, I can use 'list_%s' %(var[i]) to get 'list_a','list_b','list_c','list_d'.

My target:

for i in range(0,len(var),1):
    -----set list_var = list_var[i]--I don't know how to achieve it in code--------
    print list_var
Han Zhengzu
  • 3,694
  • 7
  • 44
  • 94

1 Answers1

0

You can use the eval function in python

list_a = [1,2,3]
list_b = [4,5,6]
list_c = [7,8,9]
list_d = [7,10,12]

letter = "a"

list = eval('list_%s' % (letter))

This question is meant to directly answer your question. Although this is how you'd dynamically generate a variable name and get its value, it is not a good practice and there are better ways to reorganise your code and make it better. (Dictionaries for example)

Alex Santos
  • 2,900
  • 1
  • 19
  • 21