Return next element in sublist

Hi All,
i’m trying to return the next element in sublists for the later use in calculations.

My input is a Nested List like this one:
image

In my Python-Script i’m searching for a certain element by parameter-value ( in this case it is the element with the ID 7325678)
Once the element is found i want to return the next element. I used the enumerate method to create a tuple and use the index to return the next element. This is working fine.

My problem being that when i try to define this as a function it is only returning the next item from the first sublist for both sublist. (see picture)
image

I can’t figure out why it is not working and would be thankful if someone might have an answer.

Greetings, Reinhard.

# Phython-Standard- und DesignScript-Bibliotheken laden
import sys
import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)


# Input aus der IN[]-Variable in die Elementliste schreiben
Elementlist = []
Testlist = []

if isinstance(IN[0],list):
    Elementlist = UnwrapElement(IN[0])
else:
    Elementlist = [UnwrapElement(IN[0])]

# Code unterhalb dieser Linie platzieren
def Methode(element):
    Methode = element.LookupParameter("Berechnungsmethode").AsString()
    return Methode
    
def Test(element):
    List = []
    for index, inner in enumerate(Elementlist):
        for idx, element in enumerate(inner):
            if Methode(element) == "Verzweigung":
                List.append(inner [idx+1])
        return List


for i in Elementlist:
    Testlist.append(Test(i))

List = []   
for index, inner in enumerate(Elementlist):
        for idx, element in enumerate(inner):
            if Methode(element) == "Verzweigung":
                List.append(inner [idx+1])


OUT = List, Testlist

First of all, do not name your function and your variable the same thing.

Second, I don’t think your custom function can reference an object that’s been defined outside the function - i.e. Elementlist isn’t defined within the Test function and therefore doesn’t exist.

I’d also recommend writing your custom functions for the simplest data structure you have. Handle all the list management in the loop and keep the function straightforward. You can get the index of an item in a list directly, which would allow you to do something like this for a single list:

def getPairs(_item,_list):
    i = _list.index(_item)
    return [_list[i],_list[i+1]]

Hi Nick, thanks for your answer.
I’m going change my functions, ckeck if it works and give a feedback.

As you said, it works for a single list, but not for a nested list. Therefore i will have to find another way. Been looking at list comprehension to solve this problem, but so far no working result.

It could work with list comprehension. However, my point is that you’re better off doing the list comprehension in the main loop, not in the function.

The issue you’re having with list comprehension is the undefined list like I mentioned and that you’re reusing variable names within the same function. You also iterate through your list in the main loop and the function in your original post. There are quite a few list comprehension issues that are unrelated to the actual logic you’re trying to apply.

1 Like

Ok, i get what you are saying. Thanks for the advice.