# \[Beginner\] Application of Lists

**URL:** <https://discourse.processing.org/t/beginner-application-of-lists/18941>\
**Category:** Processing.py\
**Created:** [March 23, 2020, 5:53pm UTC](https://discourse.processing.org/t/beginner-application-of-lists/18941 "2020-03-23T17:53:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![macedorrn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macedorrn/32/8097_2.png) [@macedorrn](https://discourse.processing.org/u/macedorrn)\
**Post date:** [March 23, 2020, 5:53pm UTC](https://discourse.processing.org/t/beginner-application-of-lists/18941/1 "2020-03-23T17:53:18Z")

</div>

So, I am trying to write a program that draws white circles at mouseX, mouseY coordinates. When I press “p”, each circle is supposed to be randomly colored individually. When I click the mouse again, more white circles are drawn and the colored ones stay the same color, and if I press p again all circles are randomly colored again.

My code draws white circles fine, but when I press “p” the color for all the circles changes as one random color(not individually), and if I draw new circles, all colors become white.  
Any help?

```auto
circle_x = []
circle_y = []
circle_size = 20

colour_r = []
colour_g = []
colour_b = []

# def circles(x,y,r,g,b):
# global circle_x, circle_y, circle_size
# fill(r,g,b)
    # ellipse(x, y, circle_size, circle_size)
    
def setup():
    size(400, 400)
    background(155)
    colour_r = [255]
    colour_g = [255]
    colour_b = [255]

def draw():
    global circle_x, circle_y, colour_r, colour_g, colour_b, circle_size
    background(155)
    fill(255)
    for z in range(0, len(colour_r)):
        fill(colour_r[z], colour_g[z], colour_b[z])
    for a in range(0, len(circle_x)):
        ellipse(circle_x[a], circle_y[a], circle_size, circle_size)
          
    
def mouseClicked():
    global circle_x, circle_y
    circle_x.append(mouseX)
    circle_y.append(mouseY)
    colour_r.append(255)
    colour_g.append(255)
    colour_b.append(255)
    
        
def keyPressed():
    if key == "p":
        for z in range(0, len(colour_r)):
            colour_r[z] = random(256)
        for a in range(len(colour_g)):
            colour_g[z] = random(256)
        for a in range(len(colour_b)):
            colour_b[z] = random(256)
            

```

---

<div class="post-metadata">

**Author:** ![macedorrn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macedorrn/32/8097_2.png) [@macedorrn](https://discourse.processing.org/u/macedorrn)\
**Post date:** [March 23, 2020, 7:45pm UTC](https://discourse.processing.org/t/beginner-application-of-lists/18941/2 "2020-03-23T19:45:28Z")

</div>

I managed to fix it!! No need for responses

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [March 24, 2020, 12:56am UTC](https://discourse.processing.org/t/beginner-application-of-lists/18941/3 "2020-03-24T00:56:38Z")

</div>

Would you like to post your own solution?

May I suggest some ideas?  
Why create 3 lists for the color components?

```python
colors = []
colors.append(color(255))

```

You could even have a single list for the circles… with a tuple inside 🙂

```python
circles = []
circles.append((mouseX, mouseY, 10, color(255)))

```

In a bigger project you might want to investigate _Named Tuples_ so you can use legible fields like `circle.x` , `circle.color` !

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 24, 2020, 1:04am UTC](https://discourse.processing.org/t/beginner-application-of-lists/18941/4 "2020-03-24T01:04:06Z")

</div>

IMO, when we’ve got a bunch of containers to describe the characteristics of a collection of objects, we should simply turn everything into 1 `class`, and then have 1 container of instances of that `class`. 💡

Although written for the Java Mode, this old article explains it w/ more details: 🤓

- [Forum.Processing.org/two/discussion/8081/from-several-arrays-to-classes](http://Forum.Processing.org/two/discussion/8081/from-several-arrays-to-classes)

---

<div class="post-metadata">

**Author:** ![macedorrn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macedorrn/32/8097_2.png) [@macedorrn](https://discourse.processing.org/u/macedorrn)\
**Post date:** [March 24, 2020, 1:24am UTC](https://discourse.processing.org/t/beginner-application-of-lists/18941/5 "2020-03-24T01:24:26Z")

</div>

Those two methods are very helpful for my other program that I’m working on! Thank you! I’ll post my solution!

---

<div class="post-metadata">

**Author:** ![macedorrn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macedorrn/32/8097_2.png) [@macedorrn](https://discourse.processing.org/u/macedorrn)\
**Post date:** [March 24, 2020, 1:26am UTC](https://discourse.processing.org/t/beginner-application-of-lists/18941/6 "2020-03-24T01:26:08Z")

</div>

I finished my program and I pasted it bellow:

```auto
#circle_x, circle_y are list that stores circles x,y coordinates
circle_x = [] 
circle_y = []
circle_size = 20 #circle diameter

#lists that stores r,g,b colors, respectivelly
colour_r = []
colour_g = []
colour_b = []
   
def setup():
    size(400, 400)
    background(155)

def draw():
    """draws circles at mouseclicked coords and assigns colours according to key 'p' pressed
    colors and coordinates are taken from the lists """
    global circle_x, circle_y, colour_r, colour_g, colour_b, circle_size
    background(155)
    fill(255)
    for z in range(0, len(colour_r)):
        fill(colour_r[z], colour_g[z], colour_b[z])
        ellipse(circle_x[z], circle_y[z], circle_size, circle_size)
          
    
def mouseClicked():
    """adds values to their list whenever mouse is clicked"""
    global circle_x, circle_y
    circle_x.append(mouseX)
    circle_y.append(mouseY)
    colour_r.append(255)
    colour_g.append(255)
    colour_b.append(255)
    
        
def keyPressed():
    """it changes the values of the lists for colours to random values"""
    if key == "p":
        for z in range(len(colour_r)):
            colour_r[z] = random(256)
            colour_g[z] = random(256)
            colour_b[z] = random(256)

```
