# Python list functions not working?

**URL:** <https://discourse.codecombat.com/t/python-list-functions-not-working/4263>\
**Category:** Bugs\
**Created:** [June 18, 2015, 11:55am UTC](https://discourse.codecombat.com/t/python-list-functions-not-working/4263 "2015-06-18T11:55:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ant](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ant/32/3362_2.png) [@ant](https://discourse.codecombat.com/u/ant)\
**Post date:** [June 18, 2015, 11:55am UTC](https://discourse.codecombat.com/t/python-list-functions-not-working/4263/1 "2015-06-18T11:55:36Z")

</div>

_Note: the following issue may be a lack of my python knowledge, or a bug (possibly linked to the issue described in the linked topic)._

_Anyway, I’d be happy to get some answers._

* * *

So. I want to manipulate lists. For example remove the enemy hero from the list of enemies; or add myself to the list of friends, etc. However, **the lists do not change**.

Examples:

```python
enemies = self.findEnemies()
self.say(enemies)
# output: Alpha, Bravo, Charlie

enemies.remove("Alpha")
self.say(enemies)
# output: Alpha, Bravo, Charlie

enemies.append("Delta")
self.say(enemies)
# output: Alpha, Bravo, Charlie

enemies.pop[0]
self.say(enemies)
# output: Alpha, Bravo, Charlie

del enemies[0]
# unexpected token

self.say(type(enemies))
# type is not defined

```

* * *

If I convert such a list using `list()` I get absolutely the same results, but in square brackets:

```python
enemies = list(self.findEnemies())
self.say(enemies)
# output: [Alpha, Bravo, Charlie]

enemies.remove("Alpha")
self.say(enemies)
# output: [Alpha, Bravo, Charlie]

# etc.

```

* * *

If I compile the list myself then `.append()` works, but the rest doesn’t:

```python
targets = self.findEnemies()
enemies = []
for target in targets:
    enemies.append(target)

self.say(enemies)
# output: [Alpha, Bravo, Charlie]

```

…or even worse: `.remove()` removes the last element, not the one in the parameter:

```python
# enemies comes from the above code
enemies.remove("Alpha")
self.say(enemies)
# output: [Alpha, Bravo] (!!!)

```

* * *

I can make a workaround by creating the lists myself, and add/exclude certain items manually. However, it’s far from the desired behavior…

---

<div class="post-metadata">

**Author:** ![trotod](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/trotod/32/3263_2.png) [@trotod](https://discourse.codecombat.com/u/trotod)\
**Post date:** [June 18, 2015, 12:29pm UTC](https://discourse.codecombat.com/t/python-list-functions-not-working/4263/2 "2015-06-18T12:29:25Z")

</div>

I don’t know how Python lists work, but when you use `findEnemies()` it returns a list of enemy objects (is that the right term?), not the names of the enemy. (I think `say` just pretty prints that for you, maybe calling the `toString()` method (JS)).

Maybe there’s a `filter` function you could use. In JS I’ve been using `Array.prototype.filter` for quite a while.

Another option would be to use list comprehensions. Probably more practical in your case.

```python
# just simple examples, they are really powerful
filtered = [enemy for enemy in enemies if enemy.type != "scout"]
notHero = [friend for friend in friends if friend.id != "Hero Placeholder"]

```

~~As for appending I’m pretty sure you could use something like~~

~~```  
friends.append(self)~~

```python
</del>

<del>Though doing something like that causes some more problems (can't command yourself if you were to command every friend)</del>
```

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [June 18, 2015, 12:31pm UTC](https://discourse.codecombat.com/t/python-list-functions-not-working/4263/3 "2015-06-18T12:31:57Z")

</div>

You can’t change the lists due to the `API-Protection`. This mechanism stops you from changing values at other objects. For example you can’t change the health of an enemy, assign to your own position (basically teleporting) or change arrays/lists you got from the system. The last one is an sad and unintended side-effect, and is as far as I know not currently tackled by any programmer.  
The programmers would have to decide for every single object they give to the player via methods or fields whether it should be protected or not. As this is way to much work _and_ new methods/fields are introduced monthly they have chosen that protecting everything was easiest.

* * *

To make it easier for you to compile the desired lists, look at this:

```python
onlyThrowers = [e for e in self.findEnemies() if e.type == "thrower"]

onlyHighHP = [e for e in self.findEnemies() if e.health >= 500 and e.health < 1000] #We don't attack too strong enemies ;)

```

---

<div class="post-metadata">

**Author:** ![ant](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ant/32/3362_2.png) [@ant](https://discourse.codecombat.com/u/ant)\
**Post date:** [June 18, 2015, 1:49pm UTC](https://discourse.codecombat.com/t/python-list-functions-not-working/4263/4 "2015-06-18T13:49:34Z")

</div>

> [@trotod](#):
>
> Another option would be to use list comprehensions

Thanks to both of you for highlighting the concept of _list comprehension!_ It seems indeed very powerful!

* * *

> [@J\_F\_B\_M](#):
>
> You can’t change the lists due to the `API-Protection`. This mechanism stops you from changing values at other objects _[…]_ or change arrays/lists you got from the system.

OK, so the API-Protection prevents me from changing the lists received from the system (e.g. `enemies = self.findEnemies()` )

But what about the lists that I created, like the `enemies` list created with the above mentioned code?

```python
targets = self.findEnemies() # this should be protected by the API
enemies = [] # but this should not be protected
for target in targets:
    enemies.append(target)

# or with my new knowledge:

enemies = [target for target in self.findEnemies()]

```

This `enemies` list is not created by the system, so it should not be protected… or am I missing something?

Thanks

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [June 18, 2015, 4:00pm UTC](https://discourse.codecombat.com/t/python-list-functions-not-working/4263/5 "2015-06-18T16:00:25Z")

</div>

Well, you **can** modify your own lists, right? I don’t know why some functions do not work as they should, but in the worst case (e.g. bug is not fixable) you could use list-comprehensions every time you want to filter something out (`[e for e in myList if e.someProperty is not someValue]`).

That is unfortunate, but I’m not fluent enough with Python to think of another (more efficient way). Can you maybe open an issue on GitHub about this, describing what and what doesn’t work as expected? Don’t include the API-protection-stuff, that is already known.

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [June 21, 2015, 12:34am UTC](https://discourse.codecombat.com/t/python-list-functions-not-working/4263/6 "2015-06-21T00:34:13Z")

</div>

Then why can I use splice and Math functions in my program?

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [June 21, 2015, 12:37am UTC](https://discourse.codecombat.com/t/python-list-functions-not-working/4263/7 "2015-06-21T00:37:27Z")

</div>

I don’t know. Maybe this operation works as intended, while others like remove doesn’t.

I don’t know where in the code the Python is translated to Javascript, but the error is possibly there.
