Python3 cannot slice a list
Assigned to Scripting category
The list you’re getting is not a Python list, but a .NET List
- I don’t know if that is intentional, @eirannejad can tell you. But until better information comes around you can use a list comprehension to convert to Python list first:
l = [i for i in x]
a = l[2:5]
or do it in one line if you prefer
a = [i for i in x][2:5]
Maybe not the nicest, but should work for now.
See upcoming changes (Rhino 8.3 RC1) here
Input lists are now IList
and subscriptable so you don’t need to convert to list anymore
@eirannejad , slicing doesn’t work, though. At least not in the very latest 8.3 build (8.3.23341.10002)
And unfortunately List<T>.Slice(Int32, Int32) Method (System.Collections.Generic) | Microsoft Learn doesn’t either. (which isn’t part of IList
, I known).
Yes that’s Python 3 (Pythonnet) bug and I have a ticket for that:
RH-78908 Python 3 can not slice dotnet lists
Slicing is more complicated to work in Pythonnet. .Slice
is a Dotnet 8 feature and unfortunately since Linq extensions are also complicated to use, you can’t use .Skip
and .Take
either.
Until this is fixed and in those cases you can:
a = list(x)[2:5]
heh, I keep forgetting to just list(x)
them XD
Ok now inputs lists are gonna be Python 3 lists automatically so they will act normally
This is a test file that tests indexing and slicing across all languages