Flatten GH Python List

Hi All,

I had this code in which x and y are set to be list type. How do I code to command it to flatten the list?

reason to flatten the list in python is I will need to post-process data in the code.

result should be like this


The code shown in the screenshot has at least two problems:

  1. in line 18 you start iterating over list x, but you never use the element i. Instead you are always using the last element of list x on line 20. Effectively the outer loop is useless,
  2. you calculate p2 in the inner loop, but you don’t do anything with it there. You append it to list box only after you’re done looping, meaning you add only one item in there,
  3. in the inner loop you enumerate over y, yet you do nothing with j - the enumerate usage is unnecessary

I’d double-check also the access type for your script component inputs. Doesn’t sound correct.

Anyway, here a sample with just two lists. Here the output a will be simply extends of the output list with straight up x and y. The b output gets what amounts to the cartesian product of two lists (with a bit of extra sauce).

simple_list_access_example.gh (8.6 KB)