4 ms·
What method would you use to iterate over a list of lists?
by feet 4y ago
What method would you use to iterate over a list of lists?
- wrigby 4y agoIt definitely doesn't solve all cases, but quite a lot of nested loops can be eliminated with `itertools.chain`: for item in itertools.chain.from_iterable(list_of_lists): # Do something with item
- hk1337 4y agoA list of lists is two deep, while not preferable two nested loops is probably going to be the solution but if I can come up with another, I would rather use that. Like, perhaps a function to process the row instead of two loops in a single function.
- feet 4y agoIf you have a function with a loop to iterate the inner list and place that function within another loop iterating the outer list, isn't that the same thing? What would you have the inner function do?