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

  • Home
  • SEARCH
  • 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 9200041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:40:39+00:00 2026-06-17T22:40:39+00:00

I have this function to split iterables into sublists by length, the fill values,

  • 0

I have this function to split iterables into sublists by length, the fill values, and the direction of fill :

def split(v,size,fill=0,direction='right'):
    if size == 0: return []
    lenv = len(v)
    count = lenv/size
    remainder = lenv%size
    result = []
    for i in range(0,lenv-remainder,size):
            result.append(v[i:i+size])
    if remainder > 0:
            if direction == 'right':
                    result.append(v[count*size:] + [fill] * (size-remainder))
            else:
                    result.append([fill] * (size-remainder) + v[count*size:])
    return result

Because I like one liners I want to rewrite it with map, but I don’t understand how to. I have this so far :

def j1(a,b): 
        return a + b 

def j2(a,b): 
        return b 

def split2(v,size,fill=0): 
        map(j1,(x for x in map(j2,v))) 

I have no idea. Any hints?

  • 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-17T22:40:40+00:00Added an answer on June 17, 2026 at 10:40 pm

    I believe you are thinking too much. This problem can be aptly handled using the grouper recipe without using map

    def split1(v,size,fill=0,direction='right'):
        result = list(izip_longest(*[iter(l)]*size, fillvalue=fill))
        if direction == 'left':
            result[-1] = result[-1][::-1]
        return result
    

    Explanation:

    • iter: This function converts a sequence to an iterable. Iterables are self consumable and has only one method, next which returns the next element from the iterable, moving from left to right.
    • [iter(l)]*size: Creates a list of size iterables
    • * (Kleene star): This operator is used to unpack a list or tuple
    • izip_longest: Transposed the elements. For shorter sequences, it is filled by filled value
    • result[-1] = result[-1][::-1]: If the direction is left reverse the last sequence

    Another possible popular solution without grouper is

    def split2(v,size,fill=0,direction='right'):
        result = [v[i:i+size] for i in range(0,len(v),size)]
        result[-1] = result[-1] + [fill] * (size - len(result[-1]))
        if direction == 'left':
            result[-1] = result[-1][::-1]
        return result
    

    Explanation:

    • Used Python’s Extended Slice. Sequence Slicing has the following syntax [start: end: stride]
    • Python Range returns a list (in Py2.x) and a range object (in Py 3.x) as a sequence/iterable , starting from start, ending at end and stepping stride elements. Similar to (for int i = start; i < stop; i+= stride)
    • [fill] * (size - len(result[-1])): Generates (size - len(result[-1])) fill elements as a list. If (size - len(result[-1])) is <=0 it generates an empty list
    • result[-1] = result[-1] + [fill] * (size - len(result[-1])) – Update the last sequence with the fill value
    • result[-1] = result[-1][::-1]: If the direction is left reverse the last sequence
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently i have this: $(.splitCol).click(function () { $.cookie('whichColumn', 'split'); $(.threeCol .active).removeClass(active); $(.leftCol .active).removeClass(active); $(.splitCol
I have this function: int firstHeapArray (IntHeapArray h) { if(!emptyHeapArray(h)) return h.array[0]; } It's
I have this function that I would like to condense into some iterator. How
I have this function //--format ISO8601 date into sections function formatDate(date){ var a =
I have this function I am using to decrypt values that works fine on
I have this warning here: Function split() is deprecated I tried replacing it with
Is there some kind of split() function in XPath? Say I have this XML:
I have got this function in my program. def time(transcriptionFile) : ''' This function
Hey guys. I have this function: i need split headers and html. string simpleGET(string
I have the following code: $('#smallcart .plusone').live('click',function(){ var id = $(this).attr('id'); articlenr = id.split('_')[1];

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.