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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:36:28+00:00 2026-05-12T08:36:28+00:00

I have the following need (in python): generate all possible tuples of length 12

  • 0

I have the following need (in python):

  • generate all possible tuples of length 12 (could be more) containing either 0, 1 or 2 (basically, a ternary number with 12 digits)
  • filter these tuples according to specific criteria, culling those not good, and keeping the ones I need.

As I had to deal with small lengths until now, the functional approach was neat and simple: a recursive function generates all possible tuples, then I cull them with a filter function. Now that I have a larger set, the generation step is taking too much time, much longer than needed as most of the paths in the solution tree will be culled later on, so I could skip their creation.

I have two solutions to solve this:

  1. derecurse the generation into a loop, and apply the filter criteria on each new 12-digits entity
  2. integrate the filtering in the recursive algorithm, so to prevent it stepping into paths that are already doomed.

My preference goes to 1 (seems easier) but I would like to hear your opinion, in particular with an eye towards how a functional programming style deals with such cases.

  • 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-05-12T08:36:28+00:00Added an answer on May 12, 2026 at 8:36 am

    How about

    import itertools
    
    results = []
    for x in itertools.product(range(3), repeat=12):
        if myfilter(x):
            results.append(x)
    

    where myfilter does the selection. Here, for example, only allowing result with 10 or more 1’s,

    def myfilter(x):  # example filter, only take lists with 10 or more 1s
        return x.count(1)>=10
    

    That is, my suggestion is your option 1. For some cases it may be slower because (depending on your criteria) you many generate many lists that you don’t need, but it’s much more general and very easy to code.

    Edit: This approach also has a one-liner form, as suggested in the comments by hughdbrown:

    results = [x for x in itertools.product(range(3), repeat=12) if myfilter(x)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 282k
  • Answers 282k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The directory structure in your answer is a little ambiguous;… May 13, 2026 at 4:03 pm
  • Editorial Team
    Editorial Team added an answer // Function to recursively add a directory, // sub-directories and… May 13, 2026 at 4:03 pm
  • Editorial Team
    Editorial Team added an answer You could add a "publication date" field in your list… May 13, 2026 at 4:03 pm

Related Questions

I have a database table with a unique string field and a couple of
I've a problem which is giving me some hard time trying to figure it
I have a MySQL database that has a few very simple tables. I would
When running.. python setup.py sdist register upload ..I get the following output: running register

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.