Iterate on a sublist for PYTHON

Sorry to bother…
Could you help me please?
I am trying to enter the list and sublists to be able to do a review but I get this image error.

How could I enter the list and sublists that I had…?? thank you so much…


instead of or use



give it a try



What I’m trying to do is enter to evaluate the sublist… but it seems that it can’t be done… what would it be like?


i am not at my computer… a other idea is indexing the sublist like i[1]



You can’t just directly iterate. You need to find out if the item is iterable, and then iterate, otherwise do the test.

The hard part is managing the output with the same structure. This is one of those situations where tackling the problem in another manor likely makes sense.

Do you mean that the first thing would be to identify if it is an element of the list or it is a sublist and based on that, continue a code to enter the sublist…???

check if your elements are lists. If they are not make a list out of the single element. Now you have a list of lists and can iterate safely.

for data in dataList:
    current_data = data if isinstance(data, list) else [data]
    for d in current_data:
        if ...:

1 Like

Thank you very much, I understand a little more how the idea should be presented.
On the other hand, if you suddenly had a list inside another and inside another and inside another… you would always keep asking yourself with the if inside another if inside another if…???

a solution with a recursive function

import sys
import clr
from Autodesk.DesignScript.Geometry import *

def create_pts(value):
    return Point.ByCoordinates(value, value, 0)

def nestedRecursFunc(lst, functoApply):
    newlst = list(lst)[:] # copy input list and convert it (if necessary)
    for idx, elem in enumerate(lst):
        if isinstance(elem, list) and len(elem) > 0:
            newlst[idx] = nestedRecursFunc(lst[idx], functoApply)
        elif isinstance(elem, list) and len(elem) == 0:
            newlst[idx] = []
            newlst[idx] = functoApply(lst[idx])

    return newlst
multi_NestedList = IN[0]

OUT = nestedRecursFunc(multi_NestedList, create_pts)

Well, you can also try this code for what you are looking for.

# Sample list
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Method 1: Using a for loop
sublist = my_list[3:7]  # Select a sublist from index 3 to 6
for item in sublist:

# Method 2: Using list slicing and a for loop
start_index = 3
end_index = 7
for item in my_list[start_index:end_index]:

# Method 3: Using list comprehension
sublist = my_list[3:7]
result = [item * 2 for item in sublist]


1 Like