Michal B.
Michal B.

Reputation: 111

Nested List-comprehension returning list of lists rather than a single list

I have a double for-loop which works perfectly fine. Given a list of categories, it fills out the "blanks" and returns a list with 1000+ URLs.

However, when converting the loop into a nested list-comprehension, it returns a list of lists.

I am aware of the possibility of flattening it, however I would like to know whether I am doing something incorrectly in the first place. I would rather prefer avoiding the extra steps of flattening it.

Above-mentioned for-loop:

for cat in categories:
    page_count = get_page_count(cat)
    for page in range(1,page_count+1):
        urls_to_scrape.append("xyz.com/" + cat + f"all?page={page}" )

The list-comprehension I attempted:

urls_to_scrape_comp = [["xyz.com/" + cat + f"all?page={page}" for page in range(1,get_page_count(cat))] for cat in categories]

As mentioned initially, the ideal outcome would be a single list using a list-comprehension

Upvotes: 1

Views: 473

Answers (2)

Simon Crane
Simon Crane

Reputation: 2182

Just remove the extra brackets and reverse the order of the comprehensions

urls_to_scrape_comp = ["xyz.com/" + cat + f"all?page={page}" for cat in categories for page in range(1,get_page_count(cat))]

Upvotes: 2

Pierre V.
Pierre V.

Reputation: 1625

Don't wrap the inner for between brackets and you should be fine:

urls_to_scrape_comp = ["xyz.com/" + cat + f"all?page={page}" for page in range(1,get_page_count(cat)) for cat in categories]

Upvotes: 0

Related Questions