Source code for eggplant.core.templatetags.partition_slice

from django import template


register = template.Library()


@register.filter
[docs]def partition(thelist, n): """ Break a list into ``n`` pieces. The last list may be larger than the rest if the list doesn't break cleanly. That is:: >>> l = range(10) >>> partition(l, 2) [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]] >>> partition(l, 3) [[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]] >>> partition(l, 4) [[0, 1], [2, 3], [4, 5], [6, 7, 8, 9]] >>> partition(l, 5) [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]] """ try: n = int(n) thelist = list(thelist) except (ValueError, TypeError): return [thelist] return [thelist[i:i+n] for i in range(0, len(thelist), n)]
@register.filter
[docs]def partition_horizontal(thelist, n): """ Break a list into ``n`` peices, but "horizontally." That is, ``partition_horizontal(range(10), 3)`` gives:: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] @see: https://djangosnippets.org/snippets/6/ """ try: n = int(n) thelist = list(thelist) except (ValueError, TypeError): return [thelist] newlists = [list() for i in range(n)] for i, val in enumerate(thelist): newlists[i % n].append(val) return newlists