Dynamo icon indicating copy to clipboard operation
Dynamo copied to clipboard

The list.reverse () method does not work in a Python dynamo node

Open andmailg opened this issue 7 years ago • 9 comments

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

andmailg avatar Sep 27 '19 18:09 andmailg

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.

mjkkirschner avatar Sep 27 '19 21:09 mjkkirschner

>>> mylist.reverse()
None

mjkkirschner avatar Sep 27 '19 21:09 mjkkirschner

Please see my test sample file. Why is an error occurring? 240_listReverse.zip

240_listReverse_2019-09-28_11-27-01

andmailg avatar Sep 28 '19 08:09 andmailg

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)

mjkkirschner avatar Sep 30 '19 03:09 mjkkirschner

Also, have you noticed a difference in behavior with 2.4 vs other dynamo versions for this behavior?

mjkkirschner avatar Sep 30 '19 03:09 mjkkirschner

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

mjkkirschner avatar Sep 30 '19 03:09 mjkkirschner

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 avatar Sep 30 '19 07:09 andmailg

@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.

mjkkirschner avatar Sep 30 '19 15:09 mjkkirschner

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.

mjkkirschner avatar Oct 02 '19 04:10 mjkkirschner