Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save junkmechanic/b5fd1aae7849e80288e469d2360584d3 to your computer and use it in GitHub Desktop.
Save junkmechanic/b5fd1aae7849e80288e469d2360584d3 to your computer and use it in GitHub Desktop.
# It is an order of magnitude slower. So dont try this at home. I wanted to save
# it since I found this list comprehension interesting when I came up with it.
import itertools
def comprehend(long_list, batch_size):
return [[u for u in k if u is not None] for k in
itertools.izip_longest(*[long_list[i::batch_size]
for i in range(batch_size)])]
def simple_split(long_list, batch_size):
lists = []
pointer = 0
while pointer < len(long_list):
lists.append(long_list[pointer:pointer + batch_size])
pointer += batch_size
return lists
# In : %timeit simple_split(long_list, batch_size)
# Out : 10000 loops, best of 3: 56.8 µs per loop
# In : %timeit comprehend(long_list, batch_size)
# Out : 1000 loops, best of 3: 432 µs per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment