# Random lists creation based on a given variable

**URL:** <https://discourse.psychopy.org/t/random-lists-creation-based-on-a-given-variable/38370>\
**Category:** Coding\
**Created:** [January 24, 2024, 2:17pm UTC](https://discourse.psychopy.org/t/random-lists-creation-based-on-a-given-variable/38370 "2024-01-24T14:17:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![J3r3my](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@J3r3my](https://discourse.psychopy.org/u/J3r3my)\
**Post date:** [January 24, 2024, 2:17pm UTC](https://discourse.psychopy.org/t/random-lists-creation-based-on-a-given-variable/38370/1 "2024-01-24T14:17:35Z")

</div>

Hi,

there is a precise list of things I’m trying to do but can’t really figure out how:

 ![Capture d'écran 2024-01-24 144111](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/5/e5832ec23798f5164cafc3a749ef91749eda88aa.png)

This is how my variables table looks like. There are 40 rows in total, each corresponding to a different verb and a different object. Verb\_type only has two modalities, and should ideally be usefull to help me make two lists.

Those lists will be used in two different loops. Each list shoud contains 20 items, an item beeing defined as a combination of a verb and an object (e.g. to watch a pencil; to grasp a saw…).

First problem (not the main one): as i don’t want to display always the same verb with a given object, a first issue is to shuffle verb and object independantly from each other, at the begining of the experiment.

Second problem, as I said above, lists should be builded according to the verb\_type variable. Each list should contain 20 items, 10 belonging to the “action” verb type, and 10 belonging to the “attention” verb type. Finally, I dont want any verb or object to be repeated, neither within or between lists. If the verb to walk is in an item of the first list it should not be in the second, if the object pencil is in list 1 it shouldn’t be in list 2… etc  
In other words, the 40 verbs and 40 objects would always be seen, by every participant. But they should not always be displayed with the same object/verb, and not always in the same list (loop).

I know almost nothing about coding. The only thing i managed to do is to make two random list with the following code, but it’s far to be enough as it doesn’t shuffle anything and doesn’t take verb\_type into account.

import random

vector = list(range(40))

RowsLoop1 = random.sample(vector, 20)  
remaining\_vector = list(set(vector) - set(RowsLoop1))

RowsLoop2 = random.sample(remaining\_vector, 20)

So, here I am. Any suggestion would be much appreciated if some of you have already dealt with comparable issues. Thank you for reading!

---

<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:** [January 24, 2024, 4:26pm UTC](https://discourse.psychopy.org/t/random-lists-creation-based-on-a-given-variable/38370/2 "2024-01-24T16:26:26Z")

</div>

Hi

Please have a look at my independent randomisation online demo.

> [@PsychoPy Online Demos](https://discourse.psychopy.org/t/psychopy-online-demos/22319):
>
> If you have routines or experiments that are likely to be useful to others, please create a post in this thread, editing your post to add new demos so there is one post per creator. Ideally set your demos so they can be tested without consuming a credit (if you don’t have a site license) by adding a final routine that can’t end, disabling save incomplete results and adding a small number of credits. Please do not discuss the demos in this thread or create more than one post unless you have a la…
