# Merge lists A and B, shuffle row-wise, then split into shuffled lists A and B

**URL:** <https://discourse.psychopy.org/t/merge-lists-a-and-b-shuffle-row-wise-then-split-into-shuffled-lists-a-and-b/23776>\
**Category:** Online experiments\
**Created:** [June 28, 2021, 10:46am UTC](https://discourse.psychopy.org/t/merge-lists-a-and-b-shuffle-row-wise-then-split-into-shuffled-lists-a-and-b/23776 "2021-06-28T10:46:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AKZ](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@AKZ](https://discourse.psychopy.org/u/AKZ)\
**Post date:** [June 28, 2021, 10:46am UTC](https://discourse.psychopy.org/t/merge-lists-a-and-b-shuffle-row-wise-then-split-into-shuffled-lists-a-and-b/23776/1 "2021-06-28T10:46:02Z")

</div>

Hi everyone,

I ran into some issues with list creation within the experiment, i.e., **merging two lists** :

> stimlist A = [“1a.jpg”,“2a.jpg”,…,“192a.jpg”]  
> stimlistB = [“1b.jpg”,“2b.jpg”,…,“192b.jpg”]

into a table with “stimlistA” and “stimlistB” as variable/column names. This table should be **shuffled row-wise/pair-wise** , resulting in something like this:

 ![grafik](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/2/a2372a8a8017e9bb671e81f0ae7bb044d8fa4216.png)

Then I would like to **split the shuffled table into two (now shuffled) lists**:

> stimlistA\_shuffled = [“2a.jpg”,“192a.jpg”,…,“1a.jpg”]  
> stimlistB\_shuffled = [“2b.jpg”,“192b.jpg”,…,“1b.jpg”]

* * *

When running on my local machine, the following Py code does the job:

> stimlist = list(zip(stimlistA,stimlistB))  
> shuffle(stimlist)  
> shuffledstimlist = stimlist  
> stimlistA\_shuffled, stimlistB\_shuffled = map(list, zip(\*stimlist))

However, I have not managed to translate this into JS to be used in an online experiment …

**What I´ve tried so far:**

> stimlist = zip(stimlistA, stimlistB);  
> shuffle(stimlist);

after inserting the following code at the beginning of the experiment:

> shuffle = util.shuffle;  
> Array.prototype.append = [].push;
> 
> const zip = (…arr) =\> {  
> const zipped = [];  
> arr.forEach((element, ind) =\> {  
> element.forEach((el, index) =\> {  
> if(!zipped[index]){  
> zipped[index] = [];  
> };  
> if(!zipped[index][ind]){  
> zipped[index][ind] = [];  
> }  
> zipped[index][ind] = el || ‘’;  
> })  
> });  
> return zipped;  
> };

URL of experiment: [https://run.pavlovia.org/fapsySRHHD/mstps\_20210628](https://run.pavlovia.org/fapsySRHHD/mstps_20210628)

Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [June 28, 2021, 7:09pm UTC](https://discourse.psychopy.org/t/merge-lists-a-and-b-shuffle-row-wise-then-split-into-shuffled-lists-a-and-b/23776/3 "2021-06-28T19:09:38Z")

</div>

Hi @AKZ , for your JavaScript, you could use

```javascript
// Create new arrays for shuffled lists
[newListA, newListB] = [[], []];
// Create new indexing array for getting stim list values by row
arrayIndex = [...Array(stimlistA.length).keys()];
// shuffle indexing array
shuffle(arrayIndex);

// Fill new lists with stimlist values shuffled by row
for (let i of arrayIndex) {
    newListA.push(stimListA[i]);
    newListB.push(stimListB[i]);
}

```

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [June 28, 2021, 7:11pm UTC](https://discourse.psychopy.org/t/merge-lists-a-and-b-shuffle-row-wise-then-split-into-shuffled-lists-a-and-b/23776/4 "2021-06-28T19:11:41Z")

</div>

Btw, this does seem like something the trial handler can do, unless you have multiple pairs that need to be shuffled

---

<div class="post-metadata">

**Author:** ![AKZ](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@AKZ](https://discourse.psychopy.org/u/AKZ)\
**Post date:** [June 29, 2021, 9:39am UTC](https://discourse.psychopy.org/t/merge-lists-a-and-b-shuffle-row-wise-then-split-into-shuffled-lists-a-and-b/23776/5 "2021-06-29T09:39:26Z")

</div>

Hi,

thank you so much for your reply!  
This helped a lot and I can follow the logic of the code. However, it only seems to shuffle newlistB but not newlistA according to the arrayIndext.  
This is the output I found in the console:  
 ![grafik](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/9/8/98a1184d97b28eb230e6ef80afb50371b598d0fc.png)

And this is what I inserted as code in the experiment:

> var newlistA;  
> var newlistB;  
> // create empty table:  
> [newlistA, newlistB] = [[], []];  
> // create indexing array for shuffling:  
> array\_shufflingIndex = […Array(stimlistA.length).keys()];  
> console.log(“array\_shufflingIndex”, array\_shufflingIndex);  
> //shuffle indexing array:  
> shuffle(array\_shufflingIndex)  
> console.log(“array\_shufflingIndex\_shuffled”, array\_shufflingIndex);
> 
> for (let i of array\_shufflingIndex) {  
> newlistA.push(stimlistA[i]);  
> newlistB.push(stimlistB[i]);  
> }

Does anyone have an idea?

I guess it would be difficult to use the trial handler here as I have 6 item sets with 384 items each and several lists need to be passed to further phases of the experiment, where they should be split again etc. … But if there is an easier way - this would be more than welcome!  
Thanks again!

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [June 29, 2021, 10:51am UTC](https://discourse.psychopy.org/t/merge-lists-a-and-b-shuffle-row-wise-then-split-into-shuffled-lists-a-and-b/23776/6 "2021-06-29T10:51:36Z")

</div>

Hi @AKZ , you might want to check that your code is not shuffling those lists elsewhere, because this code should not shuffle those lists in different orders.

---

<div class="post-metadata">

**Author:** ![AKZ](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@AKZ](https://discourse.psychopy.org/u/AKZ)\
**Post date:** [June 30, 2021, 1:44pm UTC](https://discourse.psychopy.org/t/merge-lists-a-and-b-shuffle-row-wise-then-split-into-shuffled-lists-a-and-b/23776/7 "2021-06-30T13:44:16Z")

</div>

Thanks!  
It turned out that, for some reason, swapping positions of lists A and B in the for loop solved the problem - now lists are shuffled pair-wise.

> for (let i of array\_shufflingIndex) {  
> newlistB.push(stimlistB[i]);  
> newlistA.push(stimlistA[i]);  
> }

Thanks a lot @dvbridges, your code was of great help!
