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 9230933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:56:33+00:00 2026-06-18T05:56:33+00:00

I see a few great posts here on how to split Python lists into

  • 0

I see a few great posts here on how to split Python lists into chunks like how to split an iterable in constant-size chunks.
Most posts deal with dividing up the chunks or join all strings in the list together and then limit based on normal slice routines.

However, I was in need of performing something similar based on a character-limit. If you have a list of sentences but cannot truncate any slices in the list.

I was able to churn out some code here:

def _splicegen(maxchars, stringlist):
    """
    Return a list of slices to print based on maxchars string-length boundary.
    """
    count = 0  # start at 0
    slices = []  # master list to append slices to.
    tmpslices = []  # tmp list where we append slice numbers.

    for i, each in enumerate(stringlist):
        itemlength = len(each)
        runningcount = count + itemlength
        if runningcount < int(maxchars):
            count = runningcount
            tmpslices.append(i)
        elif runningcount > int(maxchars):
            slices.append(tmpslices)
            tmpslices = []
            count = 0 + itemlength
            tmpslices.append(i)
        if i==len(stringlist)-1:
            slices.append(tmpslices)
    return slices

The output should return something like:
Slices is: [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20]]
(Each number references an item in stringlist)

So, as I iterate over this list of lists, I can use something like “”.join([item for item in each]) to print 0,1,2,3,4,5,6 on one line, 7,8,9,10,11,12,13 on another. Sometimes, a list might only be 2 items because each of those two items are very long (would add up to under the limit of 380 characters or whatever).

I know that the code is pretty bad and that I should use a generator. I’m just not sure how to do this.

Thanks.

  • 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-18T05:56:34+00:00Added an answer on June 18, 2026 at 5:56 am

    Something like this should work

    def _splicegen(maxchars, stringlist):
        """
        Return a list of slices to print based on maxchars string-length boundary.
        """
        runningcount = 0  # start at 0
        tmpslice = []  # tmp list where we append slice numbers.
        for i, item in enumerate(stringlist):
            runningcount += len(item)
            if runningcount <= int(maxchars):
                tmpslice.append(i)
            else:
                yield tmpslice
                tmpslice = [i]
                runningcount = len(item)
        yield(tmpslice)
    

    Also see the textwrap module

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

Sidebar

Related Questions

I have written a few lines of code in Python to see if I
Firstly, Modifying may be the wrong term, I see a few people have posted
I watched few videos/webcasts about Oslo but I still fail to see how it
I have a few thoughts on this but I can see problems with both.
I have an admin model with a few inline models included with it (see
I have a few pages and want that they use one style. See in
See here: http://jsfiddle.net/zYcJm/ The jQuery should be validating each field to check if they
I think the service is great but are these few the only fonts that
I installed xampp and it works great with my symfony. I even made few
I see a few question with this title already. I have checked it, if

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.