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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:57:02+00:00 2026-06-13T01:57:02+00:00

To extract all (possibly non contiguous) sublists of length r from a list li,

  • 0

To extract all (possibly non contiguous) sublists of length r from a list li, I wrote the function

def sublist(li, r):
    output = list()
    if r == 1:
        return [ [element] for element in li ]
    for firstelement in [1,len(li)-r+1]:
        output +=  [ [li[firstelement-1]] + smallerlist for smallerlist in   sublist(li[firstelement:],r-1) ]
    return output

It does not seem to work:

sage: li = [20, 17, 33, 3001]
sage: sublist(li, 2)
[[20, 17], [20, 33], [20, 3001], [33, 3001]]

Notice that sublists starting with 17 are jumped over. The problem seems to be that the counter firstelement gets modified during the recursive call. Is there a simple way to fix this?

  • 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-13T01:57:03+00:00Added an answer on June 13, 2026 at 1:57 am

    Umm, at a guess – are you trying to replicate combinations ?:

    from itertools import combinations
    list(combinations(el, 2))
    # [(20, 17), (20, 33), (20, 3001), (17, 33), (17, 3001), (33, 3001)]
    

    And you could use the “equivalent to:” section help write your own code:

    def combinations(iterable, r):
        # combinations('ABCD', 2) --> AB AC AD BC BD CD
        # combinations(range(4), 3) --> 012 013 023 123
        pool = tuple(iterable)
        n = len(pool)
        if r > n:
            return
        indices = range(r)
        yield tuple(pool[i] for i in indices)
        while True:
            for i in reversed(range(r)):
                if indices[i] != i + n - r:
                    break
            else:
                return
            indices[i] += 1
            for j in range(i+1, r):
                indices[j] = indices[j-1] + 1
            yield tuple(pool[i] for i in indices)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to extract all MP3 titles from a fuzzy list in a list.
Is it possible to extract the list of all the terms in a Lucene
I'm trying to extract all of the links to image files from a text
I'm trying to extract all the unique characters from my utf-8 French dictionary file
I am attempting to extract all instances of a particular format from a string:
I need to extract all the tags from an HTML file, In such a
I'm trying to extract all input elements from a form. When I parse the
I'm searching for a way to extract all text elements from a matplotlibfigure including
I try to extract all files from .zip containing subfolders in one folder. I
I have this specific function to extract parts of a list in the form:

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.