# Issue with .remove on array

**URL:** <https://forum.inductiveautomation.com/t/issue-with-remove-on-array/74464>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [June 1, 2023, 7:35pm UTC](https://forum.inductiveautomation.com/t/issue-with-remove-on-array/74464 "2023-06-01T19:35:24Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 7, 2023, 7:53am UTC](https://forum.inductiveautomation.com/t/issue-with-remove-on-array/74464/21 "2023-06-07T07:53:10Z")

</div>

I'm guessing you're using `pop` in a loop ? Don't do it.  
I didn't say anything about the code posted above that does it, because it breaks immediately after popping, but if you remove the break you're exposing yourself to issues like you're experiencing.

To understand why, let's take a simple example:

```python
l = [2, 1, 3, 4, 6, 5, 7, 2, 8]

print l
for i, item in enumerate(l):
	print "index {}, item {}".format(i, item)
	if item % 2 == 0:
		l.pop(i)

print l

```

output:

> [2, 1, 3, 4, 6, 5, 7, 2, 8]  
> index 0, item 2  
> index 1, item 3  
> index 2, item 4  
> index 3, item 5  
> index 4, item 7  
> index 5, item 2  
> [1, 3, 6, 5, 7, 8]

If you remove the item at index 0, the next item takes its place. So when you check at the next index... it's not what was 1 before, it's what was 2.

How to fix that ? Well, that depends on what you're trying to do.  
In most cases, I'd suggest building a new list by filtering, ie:

```python
l = filter(lambda item: item % 2, l)
# or
l = [item for item in l if item % 2]

```

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [June 7, 2023, 7:27pm UTC](https://forum.inductiveautomation.com/t/issue-with-remove-on-array/74464/22 "2023-06-07T19:27:34Z")

</div>

> [@paul-griffith](#):
>
> I think there's an existing ticket to further improve the wrapper classes that should cover this case; they just need to be smarter about their own equality.

I feel like this duck analogy is in order again...

> [@Is it possible to pass variable from gateway scope to client scope?](https://forum.inductiveautomation.com/t/is-it-possible-to-pass-variable-from-gateway-scope-to-client-scope/45008/21):
>
> One of the datatypes somewhere in the structure you’re passing into jsonEncode is more than likely not a real dict/list type and jsonEncode barfs on them. There was a [recent forum discussion](http://forum.inductiveautomation.com/t/jsonencode-issue/44603) about this failure symptom. This is a problem with [duck-typing](https://en.wikipedia.org/wiki/Duck_typing) languages in general. As I put it to my colleague, something in the structure can walk like a duck and quack like a duck, and has been passing as a duck for a very long time, but when I asked it to lay an egg it fell over and died.

[Previous page](https://forum.inductiveautomation.com/t/issue-with-remove-on-array/74464.md?page=1)
