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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:32:04+00:00 2026-06-05T08:32:04+00:00

Possible Duplicate: Rolling or sliding window iterator in Python I’m new to programming and

  • 0

Possible Duplicate:
Rolling or sliding window iterator in Python

I’m new to programming and am learning Python. I’m looking for an efficient/pythonic way to solve a problem.

I’d like a function that returns a list of iterables containing the combinations of a parent iterable as long as the elements in the combination appear the same same consecutive order as the original parent iterable.

I’m not sure if “consecutive” if the right word to describe this concept as ‘consecutive’ typically means, ‘the same element repeated.’ e.g. [1,1,1], ‘aaa’, etc…

I mean that given the list [1,2,3,4,5]:

[1,2,3] is consecutive but [1,2,4] is not. (Is there a word for this?)

Here’s a function consecutive_combinations() I created and the expected behavior:

def consecutive_combinations(iterable, consec):
    begin = 0
    chunks = len(iterable) + 1 - consec
    return [iterable[x + begin: x + consec] for x in xrange(chunks)]

def test():
    t = (1,2,3,4,5)
    s = "The quick brown fox jumps over the lazy dog."
    CC = consecutive_combinations
    assert CC(t, 2) == [(1, 2), (2, 3), (3, 4), (4, 5)]
    assert CC(t, 3) == [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
    assert CC(t, 4) == [(1, 2, 3, 4), (2, 3, 4, 5)]
    assert CC(t, 5) == [(1, 2, 3, 4, 5)]
    assert CC(s, 3) == ['The', 'he ', 'e q', ' qu', 'qui', 'uic', 'ick', 'ck ', 'k b', ' br', 'bro', 'row', 'own', 'wn ', 'n f', ' fo', 'fox', 'ox ', 'x j', '  ju', 'jum', 'ump', 'mps', 'ps ', 's o', ' ov', 'ove', 'ver', 'er ', 'r t', '    th', 'the', 'he ', 'e l', ' la', 'laz', 'azy', 'zy ', 'y d', ' do', 'dog', 'og. ']
    assert CC('', 3) == []
    print "All tests passed!"

test()

Is this an efficient solution? Is there something in itertools or some other pre-built module that would do this sort of thing?

  • 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-05T08:32:06+00:00Added an answer on June 5, 2026 at 8:32 am

    I like the pragmatic zip approach:

    n = 3
    s = "The quick brown fox jumps over the lazy dog."
    zip(*(s[i:] for i in xrange(n)))
    

    It’s not super-efficient and it only works for sequences, but often enough it does the job.

    The corresponding itertools solution is a pretty straightforward transformation of the above:

    from itertools import izip, islice, tee
    
    def slices(iterable, n):
        return izip(*(islice(it, i, None) for i, it in enumerate(tee(iterable, n))))
    

    Soo many is…

    Nevertheless, this one should work for any iterable (but may be slower for plain sequences like lists or strings).

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

Sidebar

Related Questions

Possible Duplicate: What is the most efficient way to clone a JavaScript object? (Most
Possible Duplicate: Opening url in new tab while i am doing window.open(), my page
Possible Duplicate: Python: What is the best way to check if a list is
Possible Duplicate: Modify the URL without reloading the page I'm looking for a way
Possible Duplicate: Rolling median algorithm in C Given that integers are read from a
Possible Duplicate: Unable to get a list of installed Python modules How do I
Possible Duplicate: Unload a module in Python After importing Numpy, lets say I want
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Best way to stop SQL Injection in PHP I would like to

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.