You have the mixing order of the for loops. They should be listed in nesting order, the same order you would use if you usually wrote out loops:
[x for i in range(len(l)) for x in l[i]]
If in doubt, write the loops as you would write them using statements. Your list comprehension tried to do this:
for x in l[i]: for i in range(len(l)): x
which makes it more obvious that you tried to access i before you defined it.
source share