0

How can I make this:

[char for line in grid for i,char in enumerate(line) if len(line[i:])>3]

return a list of char's for each line that meet the criteria:

[[char for line in grid] for i,char in enumerate(line) if len(line[i:])>3] #NameError: name 'line' is not defined

Solaxun
  • 2,732
  • 1
  • 22
  • 41
  • 1
    I think you want `[[char for i,char in enumerate(line) if len(line[i:])>3] for line in grid]`. Take a look at [advanced nested list comprehension syntax](http://stackoverflow.com/q/3766711/198633) – inspectorG4dget Jul 16 '15 at 16:24

1 Answers1

0

I am guessing you are looking for -

[[char for i,char in enumerate(line) if len(line[i:])>3] for line in grid]

You should move the second for loop and condition inside the list, not the first one. When there were no lists, the order of execution was - first for loop - for line in grid -> second for loop - for i,char in enumerate(line) .

The above would preserve that order, and create chars for each line meeting your condition as a separate list.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176