# Problem updating list on click

**URL:** <https://discourse.processing.org/t/problem-updating-list-on-click/25175>\
**Category:** Processing.py\
**Created:** [November 4, 2020, 3:12pm UTC](https://discourse.processing.org/t/problem-updating-list-on-click/25175 "2020-11-04T15:12:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [November 4, 2020, 3:12pm UTC](https://discourse.processing.org/t/problem-updating-list-on-click/25175/1 "2020-11-04T15:12:16Z")

</div>

Hi,

I would like to update a list of node objects (rendered as `ellipses`) every time the mouse is clicked.

The problem is that when `mouseClicked` is invoked , the `grow()` method appends an infinite amount of new nodes to the list instead of just one. To me it looks like there is some recursive process being triggered that I can’t really explain.

To sum up, what I would like:

- mouse clicked --\> last active node should append one new node to the list

what (I think) happens instead:

- mouse clicked --\> last active node appends a new node, that appends a new node, that appends a new node…

I’m sure the solution is right in front of me but today is one of those days when I need a little help.

```auto
def setup():
    size(400, 400)
    global collection

    collection = [Node(PVector(200, 200))]

def draw():
    None
    

def mouseClicked():
    drawNodes()
    
    
def drawNodes():
    background(255)
    for n in collection:
        n.grow()
        n.render()
        
    print len(collection)

class Node(object):
    def __init__ (self, pos):
        self.pos = pos
        self.isActive = True
        
    def grow(self):
        if self.isActive:
            new_pos = PVector.fromAngle(random(TWO_PI)).setMag(10).add(self.pos)
            new_node = Node(new_pos) #create new node at new position
            collection.append(new_node) #append new node to collection -> PROBLEM!!!! (run for ever instead of appending once)
            self.isActive = False #deactivate node
    
    def render(self):
        ellipse(self.pos.x, self.pos.y, 9, 9)

```

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [November 4, 2020, 3:52pm UTC](https://discourse.processing.org/t/problem-updating-list-on-click/25175/2 "2020-11-04T15:52:09Z")

</div>

Ok, I think I’m breaking the rule “never update a list that is being iterated”. Creating a temporary list instead seems to work but is not very elegant. If someone has a better workaround, I’m all ears.

```auto
def setup():
    size(400, 400)
    global collection

    collection = [Node(PVector(200, 200))]

def draw():
    None
    

def mouseClicked():
    drawNodes()
    
    
def drawNodes():
    background(255)
    global temp
    temp = []
    for n in collection:
        n.grow()
        n.render()
    collection.extend(temp) #update collection with temporary array
    print len(collection)

class Node(object):
    def __init__ (self, pos):
        self.pos = pos
        self.isActive = True
        
    def grow(self):
        if self.isActive:
            new_pos = PVector.fromAngle(random(TWO_PI)).setMag(10).add(self.pos)
            new_node = Node(new_pos) #create new node at new position
            temp.append(new_node) #append new node to temporary array 
            self.isActive = False #deactivate node
    
    def render(self):
        ellipse(self.pos.x, self.pos.y, 9, 9)

```
