Append an element to a list and del/pop/remove it after a a specific amount of time has passed since it was appended

I’m building an experiment in Psychopy in which, depending on the participants response, I append an element to a list. I’d need to remove/pop/del it after a specific amount of time has passed after it was appended (e.g. 10 seconds). I was considering creating a clock to each element added, but as I need to give a name to each clock and the number of elements created is unpredictable (dependent on the participants responses), I think I’d have to create names to each of the clocks created on the go. However, I don’t know how to do that and, on my searches about this, people usually say this isn’t a good idea.

Would anyone see a solution to the issue: remove/pop/del after a specific time has passed after appending the element?

Best,

Felipe

Hi Felipe,
I would suggest to not use a list for this purpose. Maybe it can be done using pandas.Series, using a timestamp as index. A quick example could look like this:

import pandas as pd
import datetime

my_values = pd.Series()
my_values[datetime.datetime.today()] = user_response
... # further responses added
# drop responses that are older than 10 seconds
my_values = my_values[datetime.datetime.today() - datetime.timedelta(0, 10) : ]
# if you need to have it as a list
list(my_values.values)

Best,
Robin

1 Like

hummm, very interesting… Thanks, Robin.
I’d need to have these items organized in an iteratable which I can use the items dynamically. Each item of this iteratable I’ll use do determine the position of an image which will appear in the screen when “appended” and disappear when dropped/removed.

So far, this was what I was doing, but don’t know how to remove the Barrier_position after 10 sec:

if 'participant_response':
    barrier_position = copy.deepcopy(Subject_position)
    Barrier_list.append(barrier_position)

and this will append [x, y] elements to the Barrier_list which give rise to the images of the barriers in wherever the subject(participant) was when created the barrier:

for Barrier_position in Barrier_list:
    Barrier = visual.Rect(
        win=win, name='Barrier',units='pix',
        width=(49.5, 49.5)[0], height=(49.5, 49.5)[1],
        ori=0, pos=Barrier_position,
        lineWidth=0, lineColor='black', lineColorSpace='rgb',
        fillColor=u'black', fillColorSpace='rgb',
        opacity=1, depth=-5.0, interpolate=True)
    Barrier.setAutoDraw(True)

Maybe a dictionary (Barrier_dict) using the the timestamp as key instead of the Barrier_list?

Thanks again and best regards,

Felipe