There are multiple ways to group a list/array in Python, and here are a couple of examples:
main.py157 chars6 lines
Output:
main.py23 chars2 lines
main.py122 chars5 lines
Output:
main.py23 chars2 lines
In both examples, a list of integers is grouped every 3 elements. The first example uses itertools.groupby to group consecutive elements based on the floor division by 3. The second example uses list comprehension to create sublists of length 3 using a range of indices.
gistlibby LogSnag