The list.reverse () method does not work in a Python dynamo node
Dynamo version
2.4.0.6186
Operating system
Windows 10
What did you do?
Reverse list using list.reverse() method inside Dynamo Python node
What did you expect to see?
Reversed list from OUT port
What did you see instead?
Error
https://dbader.org/blog/python-reverse-list
list.reverse() in python does not return anything - it modifies the list in place. Please share some example code if you are describing another situation.
>>> mylist.reverse()
None
I have not tried to run your file yet - but why would the same script work on different data? In one case you input List lines, in the other case a list of points?
Have you verified you have created a valid list in the first python node before trying to reverse it? (you could try returning that first)
Also, have you noticed a difference in behavior with 2.4 vs other dynamo versions for this behavior?
okay, I see now, next time, so others can benefit from this issue - please add the error so it will be searchable:
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File "<string>", line 16, in <module>
AttributeError: 'Array[Point]' object has no attribute 'reverse'
this is a very good question and has to do with ironPython lists vs .net arrays - because you reference the actual lists from the incoming data directly it's a .net array still and not a python list - so it does not have a .reverse() method - the intellisense fails you here, but it's very hard to detect this with our simple intellisense - I think that is a bug, though quite a tricky one to fix.
What you need to do here is simple, use the list constructor to convert your .net array to a python list before reversing it.
li = list(points.pop())
li.reverse()
more info here: http://www.ironpython.info/index.php?title=Typed_Arrays_in_IronPython
Thank you for your answers.
li[::-1] returnes the reversed list and can be a solution too, but what was confuse me that there was no error in the Python interpreter:
list = [[1,2,3],[4,5,6]] li = list.pop() li.reverse() print(li) [6, 5, 4]
@andmailg - it's like I mentioned above -
in the example in your last comment
list = [[1,2,3],[4,5,6]]
li = list.pop()
li.reverse()
li is a python list - you created it in python.
in your failing example, li is a list you passed into python (a regular .net list or arraylist) from dynamo.
closing for now as I think this is explained at this point, please reopen if it is not clear after looking at the attached documentation.
