Jones1220
Jones1220

Reputation: 786

Two for-loops in a list comprehension

For a project I want to get the words out of the document path that leads to afile and return them in a list. I can accomplish this by using a list comprehension, regex and a for-loop:

for path, subdir, files in os.walk(directory):
    for file in files:
        themen = [x for x in re.findall(r'[^\W\d_]+',path)]
        themen_final = []
        for i in range(4,len(themen)):
            themen_final.append(themen[i])
        print(themen_final)

This works fine, but I am sure, that one can put the for-loop for i in range... also in the list comprehension above. How do I do this?

Upvotes: 0

Views: 91

Answers (1)

Roelant
Roelant

Reputation: 5119

Just observe that with a list comprehension, the for loops take the same order as you would normally write them. So

for a in range(4):
    for b in a:
        pass

would become

[pass for a in range(4) for b in a]

With this knowledge it becomes pretty easy to rewrite any loop to a comprehension. You take the end to the front and then put the for behind it. Because you make a small list with themen_final = [] we start with:

for path, subdir, files in os.walk(directory):
    for file in files:
        themen_final = [themen for themen in re.findall(r'[^\W\d_]+',path)[:4]]

and doing the same trick once more to arrive at:

[[themen for themen in re.findall(r'[^\W\d_]+', path)[:4]] for file in files for path, _, files in os.walk(directory)]

Though I would even expect that you are either interested in re.findall(r'[^\W\d_]+', path + file) or that you could do without the for file in files. Now you would always get the same result for each file. :)

Upvotes: 1

Related Questions