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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:14:21+00:00 2026-06-18T02:14:21+00:00

I am trying to generate every combination of a dynamic character set CHAR_LIST ,

  • 0

I am trying to generate every combination of a dynamic character set CHAR_LIST, within the range of lower and upper. The code I have pasted below works, but I feel it is horribly inefficient and I would like to make it as fast as possible.

For example, if I want to generate a list between “aab” and “zzz” with only lowercase alphabetic characters it would output: ['aab', 'aac', 'aad', ..., 'zzy', 'zzz']

If there is anything I have left unclear please leave a comment and I will clarify. Thanks!

What I have working right now.

def generate_list(lower, upper):
    result = [lower]
    while lower != upper:
        if CHAR_LIST.index(lower[len(lower)-1:len(lower)]) + 1 < len(CHAR_LIST):
            lower = lower[:len(lower)-1] + CHAR_LIST[CHAR_LIST.index(lower[len(lower)-1:len(lower)]) + 1]
        else:
            new_lower = ""
            new_dig = 0
            inc_next = True
            for i in lower[::-1]:
                if i == CHAR_LIST[len(CHAR_LIST)-1] and inc_next:
                    new_lower += CHAR_LIST[0]
                    new_dig += 1
                else:
                    if inc_next:
                        inc_next = False
                        new_lower += CHAR_LIST[CHAR_LIST.index(i) + 1]
                    else:
                        new_lower += i
            if new_dig == len(lower):
                lower = str(CHAR_LIST[0])*int(len(lower)+1)
            else:
                lower = new_lower[::-1]
        result.append(lower)
    return result

EDIT: I forgot to add, as this is part of the challenge, that it must also compute a list that has different lengths for start and end points. For example it must compute the list between “a” and “zzz” also. Sorry for the late revision, and thanks for the creative answers so far 🙂

  • 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-18T02:14:22+00:00Added an answer on June 18, 2026 at 2:14 am

    It took me quite a while to understand how your code worked, because you’re doing a lot more work than you need to. Here’s an aggressively “pythonized” version of the same algorithm, which I suspect will quite a bit faster than what you have now:

    def generate_strings(value, bound, alpha):
        yield value
        while value != bound: # run until we have reached bound
            for i, c in enumerate(reversed(value)): # loop over the string in reverse
                if c != alpha[-1]: # can this character be incremented?
                    # construct an incremented value
                    value = value[:-1-i] + alpha[alpha.index(c)+1] + alpha[0]*i
                    break # exit the for loop
            else: # run only if for loop ended without breaking
                value = alpha[0]*(len(value) + 1) # make a longer string
            yield value
    

    The function is a generator, so if you want a list result, pass it to the list constructor, as in this example output:

    >>> print(list(generate_strings("b", "cc", "abcd")))
    ['b', 'c', 'd', 'aa', 'ab', 'ac', 'ad', 'ba', 'bb', 'bc', 'bd', 'ca', 'cb', 'cc']
    

    I made the sequence of characters an argument to the function, rather than using a global variable. The bound argument can also be None or some other nonsensical value to get an infinite generator (but don’t pass that to list() without shortening it!). Here’s an example of both of those features:

    >>> from itertools import islice
    >>> from string import ascii_lowercase
    >>>
    >>> print(list(islice(generate_strings("xyzzy", None, ascii_lowercase), 5)))
    ['xyzzy', 'xyzzz', 'xzaaa', 'xzaab', 'xzaac']
    

    There are a few things done in the code that might not be obvious if you’re new to Python.

    First off, I use a lot of negative indexes into the strings. This counts from the right, starting with -1 as the rightmost character. This alone would simplify your code a lot (you had a lot of x[len(x)-1]).

    Next, I use the enumerate and reversed built-in functions to loop over the string from right to left, keeping track of how many characters I’ve looped over. I think this is about what you were doing with your i and new_dig values, but I think it’s much clearer. There are a lot of helpful built in generators in Python!

    Finally, I used a break statement to exit the for loop early, with an else block to handle the case where we got to the end without breaking. This sort of else on a loop seemed useless to me when I first learned about it, but it really is handy in situations like this one, where the majority of runs of the loop will result in a break statement being hit.

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

Sidebar

Related Questions

I'm trying to generate a report of the activities that users have done within
I am trying to generate code on every build of my project using VS2012.
I am trying to generate a shortcut for every printer I have on a
I am trying to generate an MD5 check-sum of every element on a page
Im trying to generate a color gradient using ColdFusion. My current code below works
I'm trying to generate the following html code using cl-who: <html> <body> <div id=cnt_1></div>
I have been trying to generate report as per age differences of different months
I'm trying to generate 16 000 000 unique random numbers (10-digits: range 1 000
I am trying to generate a different random number every time my RandomNumber method
I'm trying to generate a call tree for code I'm working on (I didn't

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.