Skip to content

Instantly share code, notes, and snippets.

@Jonty
Created January 22, 2014 19:15
Show Gist options
  • Save Jonty/8565397 to your computer and use it in GitHub Desktop.
Save Jonty/8565397 to your computer and use it in GitHub Desktop.
A tiny function for interleaving lists with configurable amounts from each list in each chunk.
def interleave_chunks(lists):
while lists:
items, amount = lists.pop(0)
subset = items[0:amount]
items = items[amount:]
if subset:
for item in subset:
yield item
if items:
lists.append((items, amount))
if not lists:
raise StopIteration
## Example usage:
lists = [
(
range(1,10), # List
1 # Amount from list
),
(range(100,150), 2),
]
for bit in interleave_chunks(lists):
print bit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment