Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested list comprehension with os.walk

Trying to enumerate all files in a certain directory (like 'find .' in Linux, or 'dir /s /b' in Windows).

I came up with the following nested list comprehension:

from os import walk
from os.path import join
root = r'c:\windows'      #choose any folder here
allfiles = [join(root,f) for f in files for root,dirs,files in walk(root)]

Unfortunately, for the last expression, I'm getting:

NameError: name 'files' is not defined

Related to this question, which (although working) I can't understand the syntax of the nested list comprehension.

like image 552
Uri Avatar asked Oct 24 '12 14:10

Uri


People also ask

Is nested list comprehension faster than for nested loop?

As we can see, the for loop is slower than the list comprehension (9.9 seconds vs. 8.2 seconds). List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration.

Can you do nested list comprehension?

As it turns out, you can nest list comprehensions within another list comprehension to further reduce your code and make it easier to read still. As a matter of fact, there's no limit to the number of comprehensions you can nest within each other, which makes it possible to write very complex code in a single line.

What is nested list explain with example in Python?

A nested list is a list of lists, or any list that has another list as an element (a sublist). They can be helpful if you want to create a matrix or need to store a sublist along with other data types.

What is nested list comprehension?

It is a smart and concise way of creating lists by iterating over an iterable object. Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops.

What are list comprehensions in Python?

List Comprehensions are one of the most amazing features of Python. It is a smart and concise way of creating lists by iterating over an iterable object. Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops.

How do you use list comprehension in a matrix?

The list comprehension statement uses nested brackets, the range () function, and the keywords for and in to construct the statement. As you can see, the list comprehension statement takes up less space than the double for loop method of constructing a matrix.

Is list comprehension better than for loops?

Using list comprehension not only saves lines of code, but it can often be easier to read than alternative methods. While list comprehension is generally considered more “Pythonic” than other methods, such as for loops, this isn’t necessarily true.


2 Answers

You need to reverse the nesting;

allfiles = [join(root,f) for root,dirs,files in walk(root) for f in files]

See the list comprehension documentation:

When a list comprehension is supplied, it consists of a single expression followed by at least one for clause and zero or more for or if clauses. In this case, the elements of the new list are those that would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to produce a list element each time the innermost block is reached.

In other words, since you basically want the moral equivalent of:

allfiles = []
for root, dirs, files in walk(root):
    for f in files:
        allfiles.append(f)

your list comprehension should follow the same ordering.

like image 108
Martijn Pieters Avatar answered Oct 10 '22 01:10

Martijn Pieters


it is:

allfiles = [join(root, f) for _, dirs, files in walk(root) for f in files]
like image 5
SilentGhost Avatar answered Oct 10 '22 01:10

SilentGhost