|
3
|
With list comprehension, you can easily loop through all combinations of 2 or 3 lists. But what about more, like 4,6, 20? This recipe show a way to do this, without use of recursion.
At each outermost iteration, the number of result list r multiply by the number of items of current item(list). At each inner iteration, the list item i (itself is a list) is replace by a new list.
Tags: shortcuts
|
3 comments
Add a comment
Sign in to comment
Download
Copy to clipboard

Improve by using list comprehension. The lines within the outer loop could be replaced with a list comprehenstion like:
Thank you very much, I am looking for this function and helped in my work
Good work. But I'm going to play devils advocate and ask why you didn't just use itertools.product? http://docs.python.org/library/itertools.html?highlight=itertools#itertools.product