Indexes in python and get the elements in python

Very good days.
I’m still learning Python and I try to replicate the example that I always do in dynamo, which is why I don’t even know how to solve the following problema.


00_Consulta de Indices y GetIndex.dyn (16.8 KB)

1° I would like to obtain the indices that match the elements through a simple to understand code, I am learning Python.
2° I would like that through the indexes I can now obtain the elements without losing the structure of the lists.
I would be very grateful if you could help me with a simple code to understand step by step, thank you very much for your support.

hello, based on the code similar to this subject by digging the list depth
There is surely much better optimized code

import sys

lst_source=IN[0]
lst_test=IN[1]

#Container
Index_recup=[]
out_i=[]
idx_i=[]
#Code created by a beginner
for z in range(len(lst_test)):
    for i in lst_test[z]:
        for idx,j in enumerate(lst_source):
            if i==j:
                Index_recup.append(idx)
                Index_recup.sort() #Put in ascending order
    out_inter=[lst_source[k] for k in Index_recup]
    idx_inter=[k for k in Index_recup]
    Index_recup=[]
    out_i.append(out_inter)
    idx_i.append(idx_inter)

OUT = [idx_i,out_i]

Sincerely
christian.stan

Thanks for your post.
But I can’t understand what this abbreviation means.
Could you please make it more understandable in several lines… please so I can understand what it does?

1 Like

look here (more condensed writing of a loop: list comprehension)
cordially
christian.stan

when you start (like me) you put print()
it helps a lot

on the output result you enlarge your console that’s all

cordially
christian.stan

1 Like

The complicated part here is you have a list of lists, plus one object.

Generally in programming (especially written), it is not typically common to produce data structures in variable or unpredictable depths. Instead you build programs to prepare data in fairly consistent structures for predictable iteration (for/while etc.).

Having said that, data sources may sometimes be of uneven depth, e.g. split cells in tables. There are various techniques to exhaust or transform irregular list structures (e.g. flattening to a set depth), but they typically are complex or use a lot of abstracted complex code instead of being written in basic list management terms.

I’d say you’ll be far better off generally learning with uniform list depths, especially if learning Python fundamentals. Focus on for/while loops, then nested loops and conditionals, then zip statements and finally functions and lambdas. They will give you a robust set of data management tools to then direct towards the Revit API. As a fairly well structured data source, Revit will usually be giving you clean list depths to work with.

Here are some examples of dealing with non-uniform lists in the case of flattening or processing. The latter involves some intermediate to advanced concepts such as recursion (passing a function to itself, until an exiting condition is met by that function) and generators (yield, a bit like a function that can return a sequence of outcomes vs one).

https://comp.lang.python.narkive.com/dX9FK1nI/iterate-through-an-irregular-nested-list-in-python

1 Like