Creating blocks of trials with equal numbers of two image types

Hi,

I am very new to coding, and am trying to write an experiment that shows two classes of stimuli (Stimulus A and Stimulus B) for 30 seconds each, with a 30 second blank baseline period between to be used for an imaging experiment. So A - BLANK- B- BLANK - B - BLANK - A BLANK - B etc

At the moment, I have Psychopy pulling 20 random images from an excel spreadsheet and presenting them in a random order for each block of trials (based on this video https://www.youtube.com/watch?v=r0z6lGhuaEY), and saving a key press after each stimulus. This is working fine, but I don’t have a lot of control over what images are presented and when.

What I would like to achieve is to have equal numbers of A and B presented in each block, in a random order, and not having them repeated in other blocks for the same participant. Each participant would be presented with 100 stimuli in total, split into 5 groups of 20 images, and each group of 20 will have 10 images from category A and 10 from category B.

I’m pretty sure I need some sort of loop in the code to make a 2 alternative forced choice type design, but I’m not sure how to put it together. Any help or recommended resources would be much appreciated. Thank you!