8

I have a for loop like this:

for i in conversion:
    for f in glob.glob(i):
        print(os.path.getsize(f))

I want to convert this into list comprehension:

Tried this:

[os.path.getsize(f) for f in glob.glob(i) for i in conversion]

but didn't work.

pynovice
  • 6,802
  • 23
  • 67
  • 104

1 Answers1

17

The order of the for loops in a double list comprehension is the same order that you would use with nested loops:

[os.path.getsize(f) for i in conversion for f in glob.glob(i)]

It's a bit confusing because you expect the inner loop to be more "inner", but once you realize it is the same order as the nested loop, everything is easy :)

nneonneo
  • 162,933
  • 34
  • 285
  • 360
  • i am having problem while adding if statement instead of next for loop. `james_unique = [term for term in james if term not in james_unique]` – Ashfaq92 Feb 23 '19 at 08:54