1

Imagine the following piece of code which generates a list of file sizes from the current directories' files:

_sizes = []
for _base, _dirs, _files in os.walk('.'):
    for _file in _files:
        _size = getsize(join(_base, _file))
        _sizes.append(_size)

I wonder whether I can create a nested generator from this - s.th. like:

_sizes = [getsize(join(_base, _file)) 
              for _file in _files 
                  for [_base, _dirs, _files in os.walk('.')]]

This is syntactically incorrect of course since [_base, _dirs, _files in os.walk('.')] is not correct generator syntax. What I need now is a way to iterate through a sub-element from a list (_files) while referencing another one (_base) (what I called cross-referencing in the title).

I now wonder if I just can't see the obvious or if this is approach is not valid in general.

frans
  • 8,868
  • 11
  • 58
  • 132

2 Answers2

2

You can do it like this:

_sizes = [
    getsize(join(_base, _file))
    for _base, _dirs, _files in os.walk('.')
    for _file in _files
]

If you want generator, use generator expression (replacing [] with ()):

_sizes = (
    getsize(join(_base, _file))
    for _base, _dirs, _files in os.walk('.')
    for _file in _files
)

See list comprehension in Python tutorial.

falsetru
  • 357,413
  • 63
  • 732
  • 636
0

You can use following structure :

_sizes = [ getsize(join(_base, _file)) for _base, _dirs, _files in os.walk('.') for _file in _files ]

And note that you don't need to put the first loop within [] since you can use nested for loops within a list comprehension.

Mazdak
  • 105,000
  • 18
  • 159
  • 188