Skip to content

Instantly share code, notes, and snippets.

@AlmightyOatmeal
Last active May 8, 2018 22:42
Show Gist options
  • Save AlmightyOatmeal/ae00f1beee2598c80efe3e9dbea805bd to your computer and use it in GitHub Desktop.
Save AlmightyOatmeal/ae00f1beee2598c80efe3e9dbea805bd to your computer and use it in GitHub Desktop.
Fun with Python lists! Works on Python 2.7.x.
def list_chunker(list_obj, chunk_size):
"""Break apart a list into smaller lists as a generator.
:param list_obj: Array of single values in which to be chunkersized.
:type list_obj: list or set
:param chunk_size: Size of the resulting generated lists.
:type chunk_size: int
:return: List of the specified size or last remaining items.
:rtype: list
"""
[(yield list_obj[x:x+chunk_size]) for x in range(0, len(list_obj), chunk_size)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment