0

I could reproduce the problem I have in the pyqtgraph example GLLinPlotItem.py. I just added the lines

print (w.items)     

for item in w.items:
    if (type (item) == gl.GLLinePlotItem): 
        print (item)
        w.removeItem (item)

after the for loop that loads the plot with the GLLinePlotItems.

while print (w.items) prints all items my for loop prints and deletes only every second one.

I found this thread: Python for loops only every second Item but there is no solution.

Any ideas?

Thanks a lot

Martin

Edit: if you comment w.removeItem it prints all items.

  • `Modifying a set during iteration can lead to skipped elements, repeated elements, and other weirdness. Never rely on such behavior.` - See [this](https://stackoverflow.com/a/61221502/1000551) – Vadim Kotov Apr 15 '20 at 08:44
  • Hi Vadim, thanks for your answer, you got the solution, I found myself also and edited my post. – Martin_from_K Apr 15 '20 at 09:46

1 Answers1

0

Ok, I found myself: removeItem changes the list, moving every subsequent item one index forward, while the loop index remains the same. So for in loops internally are only loop through the index. I thought they take into account when the list changes. learned something new again :-) So my solution is going through the list from the rear, so removing an item does not affect the index because it counts from the front:

itm_cnt = len (w.items)
idx = itm_cnt - 1
print (itm_cnt)    
while (idx >= 0):
    if (type (w.items [idx]) == gl.GLLinePlotItem): 
        print (idx, w.items [idx])
#        w.removeItem (w.items [idx])
        del w.items [idx]
    idx -= 1

I hope this helps somebody else. Martin