How to select a different list each loop?

I’m trying to workout how to loop through three different lists of sound stimuli for each of my loops. I create them at the beginning of the experiment in code. I tried following the advice from this link
Blocks of trials and counterbalancing — PsychoPy v2022.2.4 as it has worked for me before.

I created an excel sheet with Trial_Label as my 1 parameter, with three conditions, oddballPractice, oddballPractice_Two and, oddballPractice_Three.

oddballPractice = [{'trialType': 'standard', 'trialOnset': 1},
{'trialType': 'standard', 'trialOnset': 3.12},
{'trialType': 'standard', 'trialOnset': 5.61},
{'trialType': 'oddball', 'trialOnset': 8.78},

oddballPractice_Two = [{'trialType': 'standard', 'trialOnset': 1},
{'trialType': 'standard', 'trialOnset': 3.63},
{'trialType': 'standard', 'trialOnset': 4.91},
{'trialType': 'standard', 'trialOnset': 8.38},

oddballPractice_Three = [{'trialType': 'standard', 'trialOnset': 1},
{'trialType': 'standard', 'trialOnset': 3.63},
{'trialType': 'oddball', 'trialOnset': 4.21},
{'trialType': 'oddball', 'trialOnset': 6.38},
{'trialType': 'oddball', 'trialOnset': 8.68},

I had hoped that by changing the variable name to Trial_Label it would solve the issue

thisOnset = Trial_Label[oddballTrialN]['trialOnset']
thisTrialType = Trial_Label[oddballTrialN]['trialType']

However it’s giving me an error saying it must be interger, not string, I don’t think it’s selecting the list, just the variable name.

Has anyone run into a similar issue, or know how I can get my code to update the list name each loop as the below works perfectly, but I would like it to move through the other lists each loop.

thisOnset = oddballPractice[oddballTrialN]['trialOnset']

So the basic idea is that “Trial_Label” is the variable being set by the condition file, and so you’re thinking is that its value should be, e.g., “oddballPractice” on the first block? In other words, in the excel file, the column just contains the words “oddballPractice”, “oddballPractice_Two”, and “oddballPractice_Three”?

Trial_label would therefore be a string, which would explain the error. It won’t just treat “Trial_Label” as the variable with the same name as the string. There are ways to do that, but I think the simpler solution would be to make those three lists part of a larger dict, like so:

all_trials = {'oddballPractice':[{'trialType': 'standard', 'trialOnset': 1},
{'trialType': 'standard', 'trialOnset': 3.12},
{'trialType': 'standard', 'trialOnset': 5.61},
{'trialType': 'oddball', 'trialOnset': 8.78}], 
'oddballPractice_Two':[etc.], ... } 

Then in your code:

thisOnset = all_trials[Trial_Label][oddballTrialN]['trialOnset']

Since a dictionary index is a string anyways, the fact that Trial_Label is a string read from the excel file won’t be a problem.

Hi Jonathan.

Thank you so much for the advice and explanation! I see. My python skills are not amazing, so I had an idea it was reading it purely as a string and not as the variable, but couldn’t think of a way to have it read it as a variable. I think having just a dict of three dict is a nice simple solution. I’ll test it out and see how I get on.