You can have nested iterations in a single list comprehension:
[filename for path in dirs for filename in os.listdir(path)]
which is equivalent (at least functionally) to:
filenames = []
for path in dirs:
for filename in os.listdir(path):
filenames.append(filename)
More Related Contents:
- how to create a list of lists using list comprehension [closed]
- List comprehension vs map
- List comprehension rebinds names even after scope of comprehension. Is this right?
- Creating a dictionary from a csv file?
- Python: Why is functools.partial necessary?
- Double Iteration in List Comprehension
- Can you explain closures (as they relate to Python)?
- How does functools partial do what it does?
- List vs generator comprehension speed with join function [duplicate]
- Set operations (union, intersection) on Swift array?
- How can I do assignments in a list comprehension?
- What is a Y-combinator? [closed]
- How can I use a conditional expression (expression with if and else) in a list comprehension? [duplicate]
- How to return a subset of a list that matches a condition [duplicate]
- Get number of items from list (or other iterable) with certain condition
- “Closures are poor man’s objects and vice versa” – What does this mean?
- What is the exact definition of a closure?
- The truth value of an array with more than one element is ambigous when trying to index an array
- Comprehension on a nested iterables?
- remove None value from a list without removing the 0 value
- What is “point free” style (in Functional Programming)?
- How to remove duplicate items from a list using list comprehension? [duplicate]
- sum each value in a list of tuples
- Any Functional Programming method of traversing a nested dictionary?
- Pass a typed function as a parameter in Dart
- Specification for a Functional Reactive Programming language
- How to limit the size of a comprehension?
- So: what’s the point?
- Removing syntactic sugar: List comprehension in Haskell
- Why is Python’s list comprehension loop order backwards? [duplicate]