Grasshopper Python - Problem list making because of a tuple

I’m trying to script ‘a list in one line,’ but it’s not working, what am I doing wrong?
Thanks in advance for your response.

20181013 00 problem list making.gh (73.9 KB)

You are using the enumerate function in the list comprehension, that gives a tuple of index and item. You don’t use enumerate in the first code, you’d get the same error otherwise.

Either don’t use the enumerate function in the list comprehension or ensure you access the tuple properly.

[br for i,br in enumerate(lstofbr)]
#or
[br[1] for br in enumerate(lstofbr)]
1 Like

Thank you for your response.
I do not understand how to implement that in the ‘one line’ I am trying to make. Can I separate it with comma’s?

infr_listIx = [grid_listPt.index(p) for p in enumerate(grid_listPt) for i, br in enumerate(infr_listBr) if (brep.IsPointInside(p, 0.01, True))]

Runtime error (ArgumentTypeException): expected Point3d, got tuple

Traceback:
  line 26, in script

Note that you have still a second enumerate that also needs to be handled.

... p for j, p in enumerate(...) ...

Maybe I am not smart enough, but, how? I am getting confused.
I think it should be possible to write everything in one line. However, I do not know how.

You do know how?

[grid_ListPt.index(p) for j, p in enumerate(grid_listPt) for i, br in enumerate(infr_listBR) if br.IsPointInside(p, 0.01, True)]

# or the easier, since you don't need the indices from the enumerations
[grid_ListPt.index(p) for p in grid_listPt for br in infr_listBR if br.IsPointInside(p, 0.01, True)]
1 Like

Thank you! You helped me out. :slight_smile:

Don’t forget to read through the list comprehension and make sure you understand how each part of it works. Understanding is paramount to being able to use list comprehensions to their full extent.

I have a question. About the j and the i, these are needed when working with enumerate, is that correct?

If so, I never knew that, can you tell me if that assumption is true? Then I can accept my assumption as true. That would be nice if so.

Thanks in advance for your response. :slight_smile:

When in doubt read the manual :slight_smile:

Enumerate is a built-in function of Python: https://docs.python.org/2/library/functions.html?highlight=enumerate#enumerate

1 Like