0
x=["zubin","viral","rushil"]
param=["x[0]","x[1]","x[2]"]
for paramValue in param:
    p=paramValue # here i want to read "x[0]" and want to obtain value "zubin"
    print(p) # This should print "zubin"

I want to read param and get the value of X list Your's Thankful

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149

2 Answers2

2

If you need the index and the value you can use enumerate in pyhon

for index, paramValue in enumerate(param):
    p=paramValue
    i = index 
Diana
  • 377
  • 2
  • 8
  • Actually in the situation that i got stuck is that, the parameter is coming from the database in the string format eg. {'mapSMBFEIN': {'param': ['fileToProcessRecord[columnLookup["etlfilename "]]']} } , Here i have the list of parameters for a function name mapSMBFEIN. – zubin patel Jun 25 '18 at 20:09
  • hmm therefore you could either try to extract the number of the format string or eval as suggested by @jpp – Diana Jun 25 '18 at 21:06
1

You should find a better way to reference your list, for example via integer indexing. But here's one way using eval. Note there are security risks involved, so be sure your inputs are safe.

x = ["zubin", "viral", "rushil"]
param = ["x[0]", "x[1]", "x[2]"]

for p in param:
    print(eval(p))

zubin
viral
rushil
jpp
  • 159,742
  • 34
  • 281
  • 339