# How to delete elements while traversing the list in processing.py?

**URL:** <https://discourse.processing.org/t/how-to-delete-elements-while-traversing-the-list-in-processing-py/21041>\
**Category:** Processing.py\
**Created:** [May 19, 2020, 4:06am UTC](https://discourse.processing.org/t/how-to-delete-elements-while-traversing-the-list-in-processing-py/21041 "2020-05-19T04:06:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![huahua](https://avatars.discourse-cdn.com/v4/letter/h/0ea827/32.png) [@huahua](https://discourse.processing.org/u/huahua)\
**Post date:** [May 19, 2020, 4:06am UTC](https://discourse.processing.org/t/how-to-delete-elements-while-traversing-the-list-in-processing-py/21041/1 "2020-05-19T04:06:54Z")

</div>

the code as follows is about **particles system** , and it could run successfully. i understant the code. the elements **in the list particles** represent particles, and particles will continue to be generated and die gradually, that is, traverse the list and delete the dead elements.

**But** i think **the code doesn’t meet the requestions** which the book of the Nature of Code reposes. **i want to know how to change the code to make it meet the requestions.** I appreciate your help.i hope i clarify my question.

requestions:  
1.the code could give us the next element in the list one at a time until we get to the end.  
2. And if I remove elements or move them around in the list while were iterating, we won’t look at any elements twice or skip any by accident.

The code is as follows（The key points are in the last four lines）：

```auto
class Particle(object):
    def __init__ (self,l):
        self.acceleration = PVector(0, 0.05)
        self.velocity = PVector(random(-1, 1), random(-2, 0))
        self.location = l.copy()
        self.lifespan = 255.0
        
    def run(self):
        self.update()
        self.display()
        
    def update(self):
        self.velocity.add(self.acceleration)
        self.location.add(self.velocity)
        self.lifespan -= 2.0
        
    def display(self):
        stroke(0, self.lifespan)
        strokeWeight(2)
        fill(127, self.lifespan)
        ellipse(self.location.x, self.location.y, 12, 12)
        
    def isDead(self):
        return self.lifespan < 0.0

particles = []    
def setup():
    size(800, 200)
    smooth()
    
    
def draw():
    global particles
    background(255)
    particles.append(Particle(PVector(width/2, 50)))

    for i,p in enumerate(particles):
        p.run()
        if p.isDead():
            dead_p = particles.pop(i)                                                                                 

```

**others（why i have the question and what i have done):**

i am learning the book of The Nature of Code by myself.i love the book.  
the book meets the requestion by Arraylist and iterator in java(which you could view it through [Example 4.2: ArrayList of particles with Iterator](https://natureofcode.com/book/chapter-4-particle-systems/)).

I use python to rewrite the example code in the book(the book provides java codes,but i don’t know about java.). sometimes,it is difficult for me to write the code.so i see codes from github. i am confused about this code.

i view python documents for help:

 ![1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/36b08c16709f96ec2cc0b3ee264ab7d6532cb065.png)  
**but does copying or creating a new list take up more memory?**

In addition, traversing from back to front will affect the drawing order of particles.This method is not desirable.

what should i do ?help!help!help!

---

<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:** [May 19, 2020, 4:13am UTC](https://discourse.processing.org/t/how-to-delete-elements-while-traversing-the-list-in-processing-py/21041/2 "2020-05-19T04:13:57Z")

</div>

> [@\[SOLVED\] Py.processing bullet and enemy collision](https://discourse.processing.org/t/solved-py-processing-bullet-and-enemy-collision/8327/4):
>
> @solub, your solution is buggy! bug When we delete an item from a list, all elements w/ a higher index than the deleted item got their indices subtracted by 1! face_with_monocle They’re left-shifted, and therefore, the next iteration skips an item; which now got the same index as the just deleted item. arrow_left You can see that bug in this sample loop below: eye : nums = list(range(10)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for n in nums: print n nums.remove(n) print nums # [1, …

---

<div class="post-metadata">

**Author:** ![huahua](https://avatars.discourse-cdn.com/v4/letter/h/0ea827/32.png) [@huahua](https://discourse.processing.org/u/huahua)\
**Post date:** [May 19, 2020, 10:08am UTC](https://discourse.processing.org/t/how-to-delete-elements-while-traversing-the-list-in-processing-py/21041/4 "2020-05-19T10:08:17Z")

</div>

Thank you for your attention.your are so nice.

With the help of my friend, I understand the second half of your content (from Here’s @warsoldier011’s excerpt adapted …… to the end). 😂

Maybe I didn’t make it clear that the order of elements couldn’t be changed.My friend reminded me to use a linked list。

---

<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:** [May 19, 2020, 11:18am UTC](https://discourse.processing.org/t/how-to-delete-elements-while-traversing-the-list-in-processing-py/21041/5 "2020-05-19T11:18:10Z")

</div>

> [@huahua](#):
>
> … the order of elements couldn’t be changed.

I fail to spot any place on your code where the order of Particle objects in the _particle[]_ list would matter.

Particle::**display()** method relies on property _location_ for coordinates, not on _particle[]_'s index:  
`ellipse(self.location.x, self.location.y, 12, 12)`

---

<div class="post-metadata">

**Author:** ![huahua](https://avatars.discourse-cdn.com/v4/letter/h/0ea827/32.png) [@huahua](https://discourse.processing.org/u/huahua)\
**Post date:** [June 12, 2020, 4:54am UTC](https://discourse.processing.org/t/how-to-delete-elements-while-traversing-the-list-in-processing-py/21041/6 "2020-06-12T04:54:05Z")

</div>

it was solved.thank you very very much. 😃
