List Index out of range when using coded lists for stimuli


I am creating an experiment wherein I use lists of words, which are created and checked using a code component. The end result is a list with 180 words per condition to be used across the experiment.

I then have a set of counterbalance excel sheets with the nReps of each block. Each block shows 60 of the words.There are 4 conditions (with accompanying lists) and each block is shown 3 times.

I am having two issues.

The first being, that the block does not move onto the next set of words. For example it uses the first 60 words, but when that block appears again it starts back from the first word, rather than word 61. I have tried to combat this by using .pop(0) to remove the words from the list as they’re used.

I believe this has caused the second problem, where I am getting the list index error. It runs fine until it gets to the 3rd block of a condition. Using print statements, the length of the list at this point is 60 (enough to run the block). When the count gets down to a length of 30, I get the index error. It’s confusing me as the list still has 30 words available.

I am using this code in a text component to display the words from the list


I’m not sure if the error is with the counterbalance, or with how I am removing words from the list.

When you use pop the list gets shorter. If I understand you correctly you have a list of 60 words and on each iteration you use one near the beginning and delete one from the end. After 30 iterations you now have a list with 30 items and you are asking for the 31st item.

To use .pop, try thisWord = word.pop()


Your first error seems to be related to how you are accessing the Excel sheets. Does the following example suit your needs?

CounterExp.psyexp (10.5 KB)
Counter3.xlsx (17.8 KB)
Counter2.xlsx (17.8 KB)
Counter1.xlsx (17.8 KB)

There are three lists, each with three items. Each list will be presented three times. The order of the lists is determined by the participant id.

Best wishes, Jens

I am using .pop(0) to delete the word from the start of the list rather than the end. I have also tried del word[0] but found I was having the same error.

I’ll try reworking the .pop() command and see if that works.


It does not matter whether you use .pop(0), .pop(), or del thisList[0]. All remove an element from a list. The list gets one element shorter.

Best wishes Jens

I am making the list shorter on purpose by removing the first item in the list so that it cannot show up again. This issue is that I get the index error as soon as there are 30 items left, even though it could run through those 30 items and finish the block.

I am wondering if there’s another way to work it. For example if I have:
list = [1,2,3…180]

The first 60 will show 1-60. It moves to the next block etc. When it moves back to using that list it currently uses 1-60 again. Is there a way to get it to move on without using pop or del?

Hello Jdigg

I wonder if this is really necessary, which is why I wrote my toy-example.

Best wishes Jens


The following toy example shows the numbers 1-18 in three blocks, 1-6, 7-12, 13-18. Each block is shown three times.

Counter3.psyexp (10.3 KB)
Counter3a.xlsx (17.8 KB)

Are you planning a flow like this?

Best wishes Jens

I don’t think your solution will work just because of how the experiment is setup. I’ve attached a screenshot below.

There are multiple lists involved. Because of this it doesn’t work well with excel sheets which is why I have made the lists using code. The list items for the target are different to the rest, but also need to be randomised, so coding it was simpler.

The experiment itself runs completely fine up until the index error. I just don’t know why there’s an index error, when there’s still list items available to Python to pull from.

Hello JDigg,

it does not look too difficult :blush:.

Anyway, you seem to be reusing routines, nonword_word, pseudoword_word etc., which may indicate that you are working against the design principles of PsychoPy. Having multiple routines makes it much more difficult to debug the experiment and to analyse the data, as the variables collected in each routine all go into their own column in the data file. Try to use as few routines as possible.

But I do not know enough about your experiment to be sure.

There is no such loop in the flow you posted. Where is it?

I guess you need to show us how you created the lists, randomized them and assigned them to stimuli.

Best wishes Jens


Was just from trying to test a smaller version of the experiment.

The multiple routines are fine for what we need it for, as each one contains a different set of lists.

I create the lists by:

words = [word1,...]
nonwords = [nonword1...]
#Create empty lists for NWW condition 

#Create list population for NWW 
nwwdis1 = nonwords [0:180] 
nwwdis2 = nonwords [180:360]
nwwdis3 = nonwords [360:540]
nwwdis4 = nonwords [540:720]
nwwtarget = words [0:180]

Then a similar process for the other blocks. Except pww uses words[180:360]. nww is where the index error occurs, however I believe this is just because it’s the first one to be shown for the third time.

It was an issue with saving the data. Changing from

thisExp.addData('NWW Distractor 1' , nwwdis1[nww_loop.thisN])


thisExp.addData('NWW Distractor 1' , nwwdis1[0])

Has fixed the index crash.


good that you found the fix.

Best wishes Jens