Python Forum

Full Version: iterating a list of ranges
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have multiple ranges i wish to iterate over. but the obvious solution (2 nested levels of iteration) is not available because the iteration code, which may be in a function, expects one iterator and iterates over that with just a single level (this code cannot be changed). producing a list from all the ranges and passing the list as the one iterator would work, but i want to avoid that in the list is so large it causes swapping. do i need to make a generator do this or is there a better way?

in one use case, the last range is effectively infinite, intend to make that one level iteration run "forever".
You can use itertools.chain() or itertools.chain.from_iterable()