Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:27:04+00:00 2026-06-13T21:27:04+00:00

I want to chunk an input stream for batch processing. Given an input list

  • 0

I want to chunk an input stream for batch processing. Given an input list or generator,

x_in = [1, 2, 3, 4, 5, 6 ...]

I want a function that will return chunks of that input. Say, if chunk_size=4, then,

x_chunked = [[1, 2, 3, 4], [5, 6, ...], ...]

This is something I do over and over, and was wondering if there is a more standard way than writing it myself. Am I missing something in itertools? (One could solve the problem with enumerate and groupby, but that feels clunky.) In case anyone wants to see an implementation, here it is,

def chunk_input_stream(input_stream, chunk_size):
    """partition a generator in a streaming fashion"""
    assert chunk_size >= 1
    accumulator = []
    for x in input_stream:
        accumulator.append(x)
        if len(accumulator) == chunk_size:
            yield accumulator
            accumulator = []
    if accumulator:
        yield accumulator

Edit

Inspired by kreativitea’s answer, here’s a solution with islice, which is straightforward & doesn’t require post-filtering,

from itertools import islice

def chunk_input_stream(input_stream, chunk_size):
    while True:
        chunk = list(islice(input_stream, chunk_size))
        if chunk:
            yield chunk
        else:
            return

# test it with list(chunk_input_stream(iter([1, 2, 3, 4]), 3))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:27:05+00:00Added an answer on June 13, 2026 at 9:27 pm

    [Updated version thanks to the OP: I’ve been throwing yield from at everything in sight since I upgraded and it didn’t even occur to me that I didn’t need it here.]

    Oh, what the heck:

    from itertools import takewhile, islice, count
    
    def chunk(stream, size):
        return takewhile(bool, (list(islice(stream, size)) for _ in count()))
    

    which gives:

    >>> list(chunk((i for i in range(3)), 3))
    [[0, 1, 2]]
    >>> list(chunk((i for i in range(6)), 3))
    [[0, 1, 2], [3, 4, 5]]
    >>> list(chunk((i for i in range(8)), 3))
    [[0, 1, 2], [3, 4, 5], [6, 7]]
    

    Warning: the above suffers the same problem as the OP’s chunk_input_stream if the input is a list. You could get around this with an extra iter() wrap but that’s less pretty. Conceptually, using repeat or cycle might make more sense than count() but I was character-counting for some reason. :^)

    [FTR: no, I’m still not entirely serious about this, but hey– it’s a Monday.]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some large chunk of data that I want to send to the
I've got defective input coming in that looks like this... foo<p>bar</p> And I want
I have a large amount of input data that I want to compress to
Premise: I want to take a chunk of html off of a document, and
In itext I have a chunk/phrase/paragraph (I dont mind which) and I want to
I have a binary file to which I want to append a chunk of
Want to run javascript function from parent window in child window Example I have
Want the function to sort the table by HP but if duplicate HPs then
I have 52 images that is coming from web URL.I want to display this
I have a form that I submit through ajax, and returns an updated chunk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.