0

Is there a built-in Python function such that with

vals=[1,2,3,4,5]

then foo(vals,2) gives

[[1,2],[3,4],[5]]

I am looking for the behaviour that Wolfram Language gives with

Partition[Range@5, UpTo@2]
{{1, 2}, {3, 4}, {5}}
jpp
  • 159,742
  • 34
  • 281
  • 339
Edmund
  • 488
  • 4
  • 21
  • @IsharaMadhawa That question is definitely related (I did not find it as I was searching for partition). Notable difference is ragged partitions in this case and even partitions in the linked case. – Edmund Apr 29 '18 at 12:44
  • @das-g Please post an answer. – Edmund Apr 29 '18 at 12:50
  • @Edmund It doesn't matter ragged or not, the question I mentioned was what you're looking for. It is applicable for both cases. check my answer. – Ishara Madhawa Apr 29 '18 at 12:53
  • @das-g I think `sliced` from `more-intertools` is the answer I need. Unfortunately that is not one of answers in the question this is marked as a duplicate of. The best I can do is up vote your comment. Thanks. – Edmund Apr 29 '18 at 13:10
  • @Edmund, You could also add the answer to the [duplicate](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) yourself. Having all answers on one post rather than scattered is one of SO's features. – jpp Apr 29 '18 at 16:58
  • @jpp That question has 57 answers on equally sized partitions. Is it reasonable to expect someone to; think that a question on evenly sized partitions has an answer for unevenly sized partitions, and then expect the person to search through 58 answers to find it? I don't think that is a reasonable expectation. – Edmund Apr 29 '18 at 17:10
  • We should re-open this question, as although https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks may cover unevenly sized / "ragged" partitions, too, this question is more precises by explicitly asking about them. – das-g Apr 29 '18 at 17:35
  • @Edmund, The definition of a duplicate isn't "same question" but "same answer" - in this case, the top answer is identical to the one I gave. – jpp Apr 29 '18 at 17:41
  • Did an answer below help? Feel free to accept an answer (green tick on left), or ask for clarification. – jpp May 08 '18 at 11:10
  • @das-g The question is reopened. May you add `sliced()` as an answer so I can accept. – Edmund May 08 '18 at 16:33

3 Answers3

3

You can use a list comprehension with list indexing / slicing:

vals = [1,2,3,4,5]

def foo(v, j=2):
    return [v[i:i+j] for i in range(0, len(v), j)]

print(foo(vals, 2))

[[1, 2], [3, 4], [5]]

print(foo(vals, 3))

[[1, 2, 3], [4, 5]]
PrasadK
  • 778
  • 6
  • 17
jpp
  • 159,742
  • 34
  • 281
  • 339
2

This is built into neither the Python language itself nor its standard library, but might be what you are looking for functionality-wise:

Install the third-party-library more-itertools (not to be confused with the itertools module, which is part of the Python standard library), e.g. with

pipenv install 'more-itertools >= 2.4'

Then you can use the function sliced() it provides:

from more_itertools import sliced

vals = [1,2,3,4,5]
slices = list(sliced(vals, 2))

print(slices)

Result:

[[1, 2], [3, 4], [5]]

If the iterable isn't sliceable, you can use chunked() from the same library instead of sliced.

das-g
  • 9,718
  • 4
  • 38
  • 80
1

Try this: 'n' is the subgroup size. 'l' is the list

def groups(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42