# Remove a polygon from a list and cancel its displaying

**URL:** <https://discourse.psychopy.org/t/remove-a-polygon-from-a-list-and-cancel-its-displaying/23865>\
**Category:** Coding\
**Tags:** screen\
**Created:** [July 3, 2021, 9:51pm UTC](https://discourse.psychopy.org/t/remove-a-polygon-from-a-list-and-cancel-its-displaying/23865 "2021-07-03T21:51:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avishai\_D](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/avishai_d/32/15049_2.png) [@Avishai\_D](https://discourse.psychopy.org/u/Avishai_D)\
**Post date:** [July 3, 2021, 9:51pm UTC](https://discourse.psychopy.org/t/remove-a-polygon-from-a-list-and-cancel-its-displaying/23865/1 "2021-07-03T21:51:46Z")

</div>

Hi, I’m in the process of building an experiment.

1. 

During the routine [each frame tab], circles [polygon] are added based on a long press on the ‘space’ key.

1. 

If the participant leaves the ‘Space’ key, pressing the ‘Space’ a second time will remove the circles from the list as well as their display on the screen.

I have trouble writing the code for the second step [I attached the code I wrote and a video showing its action]

```auto
keys = kb.getKeys(['space', 'return'], waitRelease = False, clear=False)

for poly in Poly_List:
    poly.setAutoDraw(True)

for thisKey in keys:
    if thisKey == 'space' and space_func: #code to change the space function
        polygon = visual.Polygon(
                  win=win, name='polygon',
                  edges=10000, size=(0.1),
                  ori=0.0, pos=(random()-0.5, random()-0.5),
                  lineWidth=1.0, colorSpace='rgb', lineColor='black', fillColor='black',
                  opacity=None, depth=0.0, interpolate=True)
        Poly_List.append(polygon) 
        core.wait(random()) 
        space_func_minus() #code to change the space function

        
    elif thisKey == 'space' and not space_func:  
         for pol in Poly_List:
            Poly_List.remove(pol)
            pol.setAutoDraw(False) 
            core.wait(random())
         space_func_plus()

```

[https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/5/15b2e5ad2f90ba65f2669b7b0cba6e3348ed394b.mp4](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/5/15b2e5ad2f90ba65f2669b7b0cba6e3348ed394b.mp4)

The problem is that the circles are not removed in sequence like the sequence that is in adding the circles. And also that the circles are removed in clumps, several at a time.

thanks!

---

<div class="post-metadata">

**Author:** ![Avishai\_D](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/avishai_d/32/15049_2.png) [@Avishai\_D](https://discourse.psychopy.org/u/Avishai_D)\
**Post date:** [July 4, 2021, 7:38pm UTC](https://discourse.psychopy.org/t/remove-a-polygon-from-a-list-and-cancel-its-displaying/23865/2 "2021-07-04T19:38:54Z")

</div>

This was the solution:

```auto
    elif thisKey == 'space' and not space_func:  
       pol = Poly_List.pop(randint(-1, len(Poly_List)-1))
       pol.setAutoDraw(False) 
       core.wait(random())
       space_func_plus()

```
