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.
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.
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.
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.
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.
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.
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.
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.
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 morefor
orif
clauses. In this case, the elements of the new list are those that would be produced by considering each of thefor
orif
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.
it is:
allfiles = [join(root, f) for _, dirs, files in walk(root) for f in files]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With