# Nested Randomized Loops without Replacement

**URL:** https://discourse.psychopy.org/t/nested-randomized-loops-without-replacement/36657
**Category:** Online experiments
**Tags:** randomization
**Created:** [October 2, 2023, 6:00pm UTC](https://discourse.psychopy.org/t/nested-randomized-loops-without-replacement/36657 "2023-10-02T18:00:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![matiasfduque](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@matiasfduque](https://discourse.psychopy.org/u/matiasfduque)
#### Post date: [October 2, 2023, 6:00pm UTC](https://discourse.psychopy.org/t/nested-randomized-loops-without-replacement/36657/1 "2023-10-02T18:00:47Z")

</div>

Hello,

I know similar questions have asked been asked before, but I have gotten myself quite confused with potential solutions. So I’m hoping to describe my problem and hopefully be pointed towards a clean solution I can try to implement.

I am looking to present two different tasks (Task 1 & Task 2) twice, under two different conditions (Cond 1 & Cond 2). Each Task has 4 possible trials (Task 1: ABCD; Task 2: EFGH). I want to show 2 of the 4 trials for each task under Condition 1, and then the remaining 2 trials for each Task under Condition 2.  
For example, a participant might see combinations such as:

- Condition 1:  
\*\* Task 1: AC  
\*\* Task 2: EG
- Condition 2:  
\*\*Task 1: BD  
\*\*Task 2: FH

Condition blocks 1 & 2 need to be randomized, but Tasks should remain in order (1 before 2).

Any idea of how to elegantly do this? I am using the Builder for online use. I can use code components if needed, but I was getting confused with that as well.

Thanks in advance!

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [October 2, 2023, 6:52pm UTC](https://discourse.psychopy.org/t/nested-randomized-loops-without-replacement/36657/2 "2023-10-02T18:52:41Z")

</div>

Here’s a solution that could work for you.

Begin Experiment

```auto
conditions = [1,2]
shuffle(conditions)
tasks = [1,1,2,2,1,1,2,2]

task1 = ['A','B','C','D']
shuffle(task1)

task2 = ['E','F','G','H']
shuffle(task2)

thisCondition = 0
thisTrial = ''

```

Add a loop called trials with no spreadsheet and nReps = 8

Begin Routine

```auto
if trials.thisN < 4:
     thisCondition = conditions[0]
else:
     thisCondition = conditions[1]

thisTask = tasks[trials.thisN]
if thisTask == 1:
     thisTrial = task1.pop() # pop assigns and then removes final item in list
else:
     thisTrial = task2.pop()

```

What you do with thisCondition (1/2), thisTask (1/2) and thisTrial (A/B/C/D) is then up to you.

---

<div class="post-metadata">

### Author: ![matiasfduque](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@matiasfduque](https://discourse.psychopy.org/u/matiasfduque)
#### Post date: [October 3, 2023, 2:58pm UTC](https://discourse.psychopy.org/t/nested-randomized-loops-without-replacement/36657/3 "2023-10-03T14:58:08Z")

</div>

Thank you! This worked great with some tweaking. The pop() function is really useful.
