Looping conditions files for two components

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): v2020.2.0
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?: I have created a Sternberg WM task with videos between the letter presentations. I have 2 components that use conditions files to present my stimuli. One .xlsx file has a list of the letters and the other has a list of the videos. There are a total of 16 trials in this experiment (so 16 letters and 16 videos). I need to randomly present the letter pairs and videos.

What did you try to make it work?: I tried to create 2 loops, 1 for each of my conditions files. I also tried using a conditions file that lists my letters and videos conditions files and used a fulLRandom loop type.

What specifically went wrong when you tried that?: I am able to randomly present the letter pairs, but the video loop shows the same video for each trial rather than a random video from the list on each trial.

Would greatly appreciate any help/resources with randomly showing the letter pairs and videos in each of the 16 trials. My apologies in advance if this has already been asked before.
PsychoPy WM task.zip (27.0 KB)


Could you shuffle the videos using a code component?

Insert a code component at the start of the experiment, have it set to “begin experiment”

In that code component write:

import random
Videos = ["videofile1",videofile2" etc] 

Where “videofile1” is replaced by the actual filename.
Then in you video component change it to


I think this should work, I am new to trying coding in PsychoPy and there may be a cleaner way to do it but I think it will work.

Additionally you may want to add a code component into your video block, set it to end routine and add:

thisExp.addData('video file', Videos[yourloopnamehere.thisN])

This will save the shown video file to your output


Thanks for your reply.
I tried your first code component and video components suggestion and it does show a different video on each trial, but the letter pair displayed before and after is the same on each trial. I need the letter pairs to change on each trial as well.

Kind regards,

You could create the lists for your word pairings as well:

pairlist = [(pair1,pair2,corrans),(p3,p3,corrans) etc

firstword =
secondword =
corrans =

for i in pairlist:

Then change your words stimuli to $firstword[yourloopnamehere.thisN] and the same for the second word. Then in your response change your correct answer to $corrans[yourloopnamehere.thisN]. Set your loop to 16, and additionally, add the thisExp for each of the words as you did for the videos.

Hi @Jdigg

I tried to insert the following code into routine ‘WM_2’ based on your example but it still shows the same letter pair with different video on each trial:

pairlist = [("D","D","z"), ("T","T","z"), ("G","G","z"), ("I","I","z"), 
    ("E","E","z"), ("J","J","z"), ("F","F","z"), ("P","P","z"), 
    ("Y","K","slash"), ("B","T","slash"), ("U","N","slash"), ("Z","K","slash"),
    ("K","V","slash"), ("N","F","slash"), ("V","L","slash"), ("O","S","slash")]

firststim = []
secondstim = []
corrAns = []

for i in pairlist:

I also added the following code in the End Routine:

thisExp.addData('pairlist', firststim[wm_trials.thisN])
thisExp.addData('pairlist', secondstim[wm_trials.thisN])

I’ve reattached the experiment files with the 2 code components in case you would like to take a look.
WM Task v1_lastrun.py (33.1 KB)
ExpTrial_Ped.xlsx (9.0 KB)
letters.xlsx (15.8 KB)
WM Task v1.psyexp (30.3 KB)

Thanking you,


Could you try the attached file please?

If you’d like to try making the changes yourself I have listed the changes made below:

  • Moved all the list creation code to a code block at the start of the experiment. I think this is where you were having the issues with the error message you were getting.
  • Removed the video loop, using the code components means this is no longer necessary, unless I am misunderstanding the experiment
  • Changed mention of the video loop to the wm loop in the code, as the video loop was removed
  • Changed your thisExp saving from ‘pairlist’ to ‘firststim’ and ‘secondstim’, because otherwise it was only saving one of the letters

WM Task v1.psyexp (31.5 KB)

This works perfectly @Jdigg! Thanks a lot for your help, I really appreciate it :smiley: