-1

This question is about List Comprehension. However, the iterables are a bit complex so I am confused about how to write this List Comprehension.

So, I have Some pydantic class like this

class textbox(BaseModel):
    x1: int
    y1: int
    x2: int
    y2: int
    
class Page(BaseModel):
    color: List[RGB]
    textboxes: List[textbox]
    
class Notepad(BaseModel):
    pages: List[Page]

Assuming all of them have appropriate values assigned to them. I want to do a List Comprehension like this so that I can access each of the Page separately and for every Page access each of the textbox and put them inside one list.

TotalTextBox = [textbox in textboxes for Notepad.pages[i].textboxes] 

But I am facing a problem because List Comprehension is not letting me use the counter variable i. Can anyone please help me with this problem? Thank you.

P.S. I don't want to use enumerate. I want a simple list as result.

deceze
  • 510,633
  • 85
  • 743
  • 889
shantanu rahut
  • 189
  • 1
  • 4
  • 15
  • There's a function that takes a list as a parameter and returns tuples of `(index, element)` for each element... can't remember its name. – Haroldo_OK Oct 05 '21 at 10:06
  • 2
    @Haroldo_OK: You mean `enumerate()`. – quamrana Oct 05 '21 at 10:06
  • Hi @Haroldo_OK , I have found something called enumerate, it is a builtin function of python. But it returns enumerate object as I understand. What I want is a simple List. – shantanu rahut Oct 05 '21 at 10:07
  • 4
    `[tb for page in notepad.pages for tb in page.textboxes]`…? – deceze Oct 05 '21 at 10:07
  • Perhaps you could first try a normal `for` loop to make sure you can get what you want. Lots of for loops can be converted to list comprehensions. – quamrana Oct 05 '21 at 10:07
  • What's wrong with just iterating over the (list of) pages as well? You obviously know how to iterate over the (list of) textboxes – same thing. – MisterMiyagi Oct 05 '21 at 10:08
  • 1
    An enumerated object can be used in a list comprehension, and in fact, in most places you would use a list; no problems there. – Haroldo_OK Oct 05 '21 at 10:08
  • 1
    Yes, while `enumerate()` returns an object, it is designed to be iterated over. There are loads of [examples](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops/522578#522578) on this website. – quamrana Oct 05 '21 at 10:10
  • 3
    It would be helpful if you could provide and update your question with example data to help users reproduce your issue. – Aidan Donnelly Oct 05 '21 at 10:14

1 Answers1

0

You would wish to use enumerate function in Python.

I would advise to start simple, and use multiple for loops as you know how to implement this.

Then slowly move this into a comprehensive list. It is often easier to break down your problem into stages and transition this into your desired answer.

An example of how enumerate works with list comprehension to apply this to your problem.

a_list = ["a", "b", "c"]
enumerate_list = [(index, element) for index, element in enumerate(a_list)]

Printing the list would look like follows:

print(enumerate_list)
[(0, 'a'), (1, 'b'), (2, 'c')]

As per suggestion of Haroldo, to prove this does in fact provide your final result of a list, if you print the type like this:

print(type(enumerate_list))

The output will be like such:

<class 'list'>

This can be modified to suit your need, just apply the knowledge of your lists and take it slowly.

Aidan Donnelly
  • 369
  • 5
  • 19
  • As per the question: "I *don't* want to use enumerate." – MisterMiyagi Oct 05 '21 at 10:11
  • 2
    This question was seemingly edited whilst I posted this answer. – Aidan Donnelly Oct 05 '21 at 10:13
  • 2
    As far as a can see, the reason they don't want to use `enumerate()` is because they don't understand how and why it would work. But work, it would: the end result would be a list; the intermediate type does not matter. Maybe you could add an extra line printing the type of `enumerate_list` showing that it is, in fact, a list; it's clear to me, but might not be for others. – Haroldo_OK Oct 05 '21 at 10:18
  • 3
    I have made this change to hopefully point them in the right direction. As it would definitely provide them with what they are after. – Aidan Donnelly Oct 05 '21 at 10:23
  • I forgot to explain more clearly the syntax details: `enumerate(a_list)` does return an enumerated object, but the expression around it, `[(index, element) for index, element in some_enumerated_object]` returns an list. In fact, if you wanted to make the whole comprehension return an enumerated object instead of a list, you would replace the square brackets `[]` with parentheses `()`. – Haroldo_OK Oct 05 '21 at 10:33