0
$\begingroup$

I'm stuck with an issue which is actually solved in the documentation of Animation Nodes here but as I'm still learning python I can't find the error.

This expression should append elements to a list after a condition:

[filas.append(x) for x in list if x == sector_name]

But as in the docs is explained the return will be none we have to add the list created like this:

[list.append(element), list][1]

but I can't figure out how to write my expression correctly, please some help.

$\endgroup$
2
$\begingroup$

The list comprehension you have will return a list of None, instead you want the actual list filas after the appending was done. The documentation describes the case where you append a single element, so all you have to do is replace the first element of the dummy list with your list comprehension as follows:

[[filas.append(x) for x in list if x == sector_name], filas][1]

Note, however, that such expression is not required, you can use a normal list comprehension as follows:

[x for x in list if x == sector_name]
$\endgroup$
2
$\begingroup$

Lists can be joined with "+": So you make a new list (list comprehension) with all the x for which the condition is True and simply add it to the existing list.

filas + [x for x in list if x == sector_name]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.