Python nested list help

import rhinoscriptsyntax as rs 

LineList = x
PtList = y
result=[]
count = 0
for a in LineList:
    for b in PtList:
        test = rs.IsPointOnCurve(a,b)
        result[count].append(test)
    count += 1
a = result

Can anyone tell me why this doesn’t work.

Python’s for loop is pretty smart, in that it iterates through any iterable, not just a list of numbers. So you really don’t need to use a count variable for this situation (see https://wiki.python.org/moin/ForLoop). Also when you append a value to a list, you just reference the list, not the index you are appending it to. The append function always adds data to the end of the list. Line 10 should be result.append(test). If you are seeking to generate a nested list within the result variable, you need to assign a list within the first for loop, append values to that list, and append the list to result.

Also it helps not to overassign variables, you are using a in your loop as well as a (which is the default ghpython output variable) for your result in line 12. You should change one of them so that you don’t lose stored data.

Thanks for the help. A nested list is what I was after so I was trying use the count to achieve that, thinking that I could use it to define the first list, second list ect…


import rhinoscriptsyntax as rs 

LineList = x
PtList = y
result=[]
for i in LineList:
    testlist=[]
    for ii in PtList:
        test = rs.IsPointOnCurve(i,ii)
        testlist.append(test)
    result.append(testlist)
print(a)
a=result

Yeah this works better thanks.

1 Like