# Changing stimuli presented each loop

**URL:** <https://discourse.psychopy.org/t/changing-stimuli-presented-each-loop/43411>\
**Category:** Builder\
**Created:** [January 8, 2025, 12:42am UTC](https://discourse.psychopy.org/t/changing-stimuli-presented-each-loop/43411 "2025-01-08T00:42:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ar3](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@ar3](https://discourse.psychopy.org/u/ar3)\
**Post date:** [January 8, 2025, 12:42am UTC](https://discourse.psychopy.org/t/changing-stimuli-presented-each-loop/43411/1 "2025-01-08T00:42:55Z")

</div>

**OS** (e.g. Win10): Windows 11  
**PsychoPy version** (e.g. 1.84.x): 2024.2.4  
**Standard Standalone? (y)** If not then what?:  
**What are you trying to achieve?:**  
I have 48 stimuli in an excel sheet. 8 stimuli need to be presented sequentially every “fullexp” loop (total:6). The group of 8 stimuli should be different every loop. But, the group of eight words presented in triala1, triala2, triala3, and triala4 should be the same the entire loop. The 4 trial loops also have code in the Num. repeats slot for counterbalancing purposes, so I believe I need code for this solution but I have not been successful so far. Any help would be greatly appreciated.

 ![IMG_0812](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/9/4/94a4fdc13a4ae1ac98c82ddf433d44ea315110ef.jpeg)

**What did you try to make it work?:**

1. created a code component with the following code

> [@Free recall task - lists of words](https://discourse.psychopy.org/t/free-recall-task-lists-of-words/35447/2):
>
> ```auto
> useRows = '0:1'
> wordList = ['0:15','15:30','30:45','45:60']
> [details="Summary"]
> This text will be hidden
> [/details]
> 
> shuffle(wordList)
> 
> ```

> [@Free recall task - lists of words](https://discourse.psychopy.org/t/free-recall-task-lists-of-words/35447/2):
>
> `useRows = wordList.pop()`

1. created a code component with the following code: [quote=“JensBoelte, post:2, topic:35279”]

```auto
if block.thisRepN == 0:
    useRows = '2:5'
elif block.thisRepN == 1:
    useRows = '6:9'

```

[/quote]  
and initialized it

> [@Selecting different rows per iteration in a loop - updated advice?](https://discourse.psychopy.org/t/selecting-different-rows-per-iteration-in-a-loop-updated-advice/35279/2):
>
> `useRows = '0:2'`

1. I have also added

> if trials.thisN ==7:  
> trials.finished = True

**What specifically went wrong when you tried that?:**

1. This worked initially, but soon it gave an IndexError: pop from an empty list. and the experiment would not continue.
2. Unfortunately, this did not change the words every loop.
3. The words stayed the same every loop.

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [January 8, 2025, 8:22am UTC](https://discourse.psychopy.org/t/changing-stimuli-presented-each-loop/43411/2 "2025-01-08T08:22:29Z")

</div>

Hello

> [@ar3](#):
>
> This worked initially, but soon it gave an IndexError: pop from an empty list. and the experiment would not continue.

This indicates that you have tried to read an element from an empty list. **Yourlist.pop()** removes the last element from your list. So each time you **pop()** your list gets shorter by one element until there is no element left. You are calling yourlist.pop() one too many times.

Am I right in assuming that you have modified the code to suit your needs?

> [@ar3](#):
>
> - Unfortunately, this did not change the words every loop.
> - The words stayed the same every loop.

Does that mean that you say the same set of eight words in all relevant routines despite the fact that your looped over the the routines six times?

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [January 8, 2025, 10:51am UTC](https://discourse.psychopy.org/t/changing-stimuli-presented-each-loop/43411/3 "2025-01-08T10:51:56Z")

</div>

Hello @ar3

OK, I hope I have understood your experimental procedure correctly.

Insert a routine before your loop **fullexp**. In this routine, add a code component and specify the following in the **Begin experiment** tab of this code component

```auto
useRows = '0:1'
stimList = ['0:8','8:16','16:24','24:32','32:40','40:48']

shuffle(stimList) #if you want random block presentation

```

Set the loop **fullexp** to 6 iterations. Leave the condition file empty.

Add a routine containing a code component as the first routine in the **fullexp** loop. In the **begin routine** tab of this code component, add

```auto
useRows = stimList.pop()

```

Then add your stimulus presentation routines, each surrounded by its loop, and **set repetition** to 1. Set the **condition file** to the Excel file containing your stimuli, and set the **Selected rows** parameter to **$useRows**. If you want a random presentation of the stimuli in a block, set the **Loop type** to random.

Best wishes, Jens

---

<div class="post-metadata">

**Author:** ![ar3](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@ar3](https://discourse.psychopy.org/u/ar3)\
**Post date:** [January 8, 2025, 11:33pm UTC](https://discourse.psychopy.org/t/changing-stimuli-presented-each-loop/43411/4 "2025-01-08T23:33:34Z")

</div>

Thank you so much! This worked
