Reverse List Using Python Node

How can you reverse lists within the python node?

I also tried EL[::-1] without any luck.

You need to define what the reverse is to output it.

In like 8 you set EL to the input.

In line 9 you reverse the list but do not set it to a variable.

In line 11 you set the OUT to EL.

Try adding a variable to the reversed value, changing the line to something like reversedEL = EL.reverse(), and then set the out to reversedEL.

You are providing it a list of lists. The outer list only contains a single item, therefore the reverse is identical to the original list. For example:

>>> data1 = [0, 1, 2]
>>> data1.reverse()
>>> print data1
[2, 1, 0]
>>> data2 = [[0, 1, 2]]
>>> data2.reverse()
>>> print data2
[[0, 1, 2]]

@JacobSmall I attempted that first and still no luck.

@cgartland should I just flatten the list completely? Since it could be a list of two or three depending on the situation. Is there an other way to ensure the list with values is the one being reversed?

You would most likely have to wrap it in a custom node definition and let Dynamo handle the lacing/levels. Otherwise, you can write a recursive function to do this directly within your Python node as-is.

Hi @Smaren @cgartland @JacobSmall

You can do with one line of python code this way:

OUT = map(lambda x: x[::-1], IN[0])

4 Likes