# Subsetting Rows from Conditions File Is Not Working

**URL:** <https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579>\
**Category:** Builder\
**Created:** [May 21, 2020, 9:22pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579 "2020-05-21T21:22:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngallagher](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ngallagher/32/3383_2.png) [@ngallagher](https://discourse.psychopy.org/u/ngallagher)\
**Post date:** [May 21, 2020, 9:22pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/1 "2020-05-21T21:22:32Z")

</div>

**OS** (e.g. Win10): Mac OS10  
**PsychoPy version** (e.g. 1.84.x): 2020.1.2  
\*\*Standard Standalone? Yes

**What are you trying to achieve?:**  
I have a practice trial part of my experiment. In builder, I set this up as a loop that references a .csv file with trial information for 48 possible trials, in 48 rows. However, I want to randomly select only 5 of those 48.

**What did you try to make it work?:**  
I tried the advice from the main psychopy site under “flow”, which says to set Selected Rows to random(desired number of trials)\*total rows in csv.

I have also tried suggestions from from two different threads on here: [one](https://discourse.psychopy.org/t/how-to-present-subset-of-all-images-randomly/2591/8), [two](https://discourse.psychopy.org/t/randomly-pick-up-an-image-from-a-big-pool-of-images-all-belonging-to-a-condition/11708/7). I also tried a few different configurations of loopType and Selected Rows, but nothing has worked.

Images of things I’ve tried are below.

**What specifically went wrong when you tried that?:**  
When I use the advice from the main psychopy site, I get this error: " for n in selection:  
TypeError: ‘NoneType’ object is not iterable"

When I use the advice from the forum posts (the added code snippet), I do not get an error, but the experiment runs through all the rows in my practrials.csv file.

 ![Screen Shot 2020-05-21 at 4.18.14 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/2/d27bc787ace074f7e3bc3c7e9f24cc58bad7c7e1.png)

 ![Screen Shot 2020-05-21 at 4.18.46 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/7/c72d4b9d31b20eb346b2a719ea393f0616027948.png)

Thanks for any guidance! After trying all the things I could find, I’m really not sure why this isn’t working for me.

- Natalie

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [May 22, 2020, 8:39am UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/2 "2020-05-22T08:39:32Z")

</div>

Hi There,

So I think that if you were going to do it as in post 1 you wouldn’t need any input to selected rows. (i.e. remove random(4)\*47).

What this will mean is you are essentially feeding in all trials, in a random order, but once you hit the 5th trial the practrials loop should end.

Good luck!  
Becca

---

<div class="post-metadata">

**Author:** ![ngallagher](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ngallagher/32/3383_2.png) [@ngallagher](https://discourse.psychopy.org/u/ngallagher)\
**Post date:** [May 22, 2020, 2:36pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/3 "2020-05-22T14:36:12Z")

</div>

Hi Becca,

Thanks for your reply! I wasn’t clear enough - I’ve followed both methods separately (e.g. just the code snippets without the random() setting, and just the random() setting without the code snippets). I haven’t been combining the two methods, since they’re separate recommendations.

When I have _just_ the random() code in Selected Rows (per the main site), I get an error: “for n in selection: TypeError: ‘NoneType’ object is not iterable”

When I have _just_ the code snippet added to the routine (per the forum recommendation), the loop doesn’t stop after 5 trials. Instead, it goes through all 48 possible practice trials.

Thanks in advance for any other guidance!

Best,  
Nat

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [May 22, 2020, 4:00pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/4 "2020-05-22T16:00:52Z")

</div>

Hi Nat,

Ok so for method 1: ““for n in selection: TypeError: ‘NoneType’ object is not iterable”” means that you are not getting a list to iterate through - have you tried it with a $ in front of it? alternatively to debug that error in future try printing the output by adding print(random(4)\*47) to a code snippet at the start.

For the second method: I think I might see the issue you might have to change practrials.thisN to practrials.thisTrialN (check the variable name in the header of your .csv data file).

Let me know how you get on,  
Becca

---

<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:** [May 22, 2020, 4:05pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/5 "2020-05-22T16:05:26Z")

</div>

Hi

Just to check. Are you testing this locally or online?

Best wishes

Wakefield

---

<div class="post-metadata">

**Author:** ![ngallagher](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ngallagher/32/3383_2.png) [@ngallagher](https://discourse.psychopy.org/u/ngallagher)\
**Post date:** [May 22, 2020, 5:04pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/6 "2020-05-22T17:04:56Z")

</div>

Adding a $ to the random command did it! Thanks so much Becca!

Wakefield, this was running on a local computer.

Best,  
Nat

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [May 22, 2020, 5:44pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/7 "2020-05-22T17:44:37Z")

</div>

Woo good to hear! Could you tick the solution for future users?!

Thanks 😊

---

<div class="post-metadata">

**Author:** ![ngallagher](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ngallagher/32/3383_2.png) [@ngallagher](https://discourse.psychopy.org/u/ngallagher)\
**Post date:** [May 22, 2020, 7:08pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/8 "2020-05-22T19:08:56Z")

</div>

Done! I didn’t realize that was an option, first time on this forum 😅

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [May 22, 2020, 7:09pm UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/9 "2020-05-22T19:09:41Z")

</div>

No worries! Good luck with the rest of your study!

---

<div class="post-metadata">

**Author:** ![ricslator](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@ricslator](https://discourse.psychopy.org/u/ricslator)\
**Post date:** [September 13, 2022, 6:28am UTC](https://discourse.psychopy.org/t/subsetting-rows-from-conditions-file-is-not-working/13579/10 "2022-09-13T06:28:32Z")

</div>

> [@ngallagher](#):
>
> TypeError: ‘NoneType’ object is not iterable"

TypeError: ‘NoneType’ object is not iterable

This error means that python is trying to iterate over a None object. With Python, you can only iterate over an object if that object has a value. This is because iterable objects only have a next item which can be accessed if their value is not equal to None. If you try to iterate over a None object, you encounter the TypeError: ‘NoneType’ object is not iterable error.

For example, [list.sort()](http://net-informations.com/python/ds/listsort.htm) only change the list but return None.

Python’s interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the ` __iter__ ` method on the None. None has no ` __iter__ ` method defined, so Python’s virtual machine tells you what it sees: that NoneType has no ` __iter__ ` method.

Technically, you can avoid the NoneType exception by checking if a value is equal to None before you iterate over that value.
