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

The Archive Base Latest Questions

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

According to http://docs.python.org/2/library/itertools.html#itertools.product the following function is equivalent to using their library (I removed

  • 0

According to http://docs.python.org/2/library/itertools.html#itertools.product the following function is equivalent to using their library (I removed a few things I don’t need from it):

def product(*args):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    pools = map(tuple, args)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

In my case I’m passing the product function 3 lists, but I need to add some conditional checks, so it doesn’t mix certain items from one list with items in another list if they don’t meet the requirements. So what I figured I need to do is convert:

result = [x+[y] for x in result for y in pool]

into “normal” FOR loops (not sure how to refer to them), so I can add several IF checks to verify whether the items in the lists should be mixed together or not.

What mainly confuses me is that “x” is iterating through the “result” list which is empty, but items are added to it as it iterates, so I think this is what is complicating the conversion to normal loops for me.

Here is one of my attempts:

def product(*args):
    pools = map(tuple, args)
    result = [[]]
    for pool in pools:
        for x in result:
            for y in pool:
                result.append(x+[y])
    for prod in result:
        yield tuple(prod)

Any help is greatly appreciated!

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

    You’re very close: the right hand side of a nested list comprehension is written in the same order that you’d write the for loops, so you’ve got that right. However, in the listcomp version, first the RHS of the assignment is computed and then it’s bound to the name on the LHS. So

    result = [x+[y] for x in result for y in pool]
    

    needs to become

    new_result = []
    for x in result:
        for y in pool:
            new_result.append(x+[y])
    result = new_result
    

    So that you’re not modifying result as you iterate over it. If you wanted to forbid certain arrangements — and you can write your constraint in such a way that it works for that iteration order, which fills in from left-to-right — then you could do this:

    def filtered_product(args, filter_fn):
        pools = map(tuple, args)
        result = [[]]
        for pool in pools:
            new_result = []
            for x in result:
                for y in pool:
                    new_val = x+[y]
                    if filter_fn(new_val):
                        new_result.append(x+[y])
            result = new_result
            print 'intermediate result:', result
        for prod in result:
            yield tuple(prod)
    

    which gives

    In [25]: list(filtered_product([[1,2,3], [4,5,6], [7,8,9]], lambda x: sum(x) % 3 != 2))
    intermediate result: [[1], [3]]
    intermediate result: [[1, 5], [1, 6], [3, 4], [3, 6]]
    intermediate result: [[1, 5, 7], [1, 5, 9], [1, 6, 8], [1, 6, 9], [3, 4, 8], [3, 4, 9], [3, 6, 7], [3, 6, 9]]
    Out[25]: 
    [(1, 5, 7),
     (1, 5, 9),
     (1, 6, 8),
     (1, 6, 9),
     (3, 4, 8),
     (3, 4, 9),
     (3, 6, 7),
     (3, 6, 9)]
    

    Whether or not this gives you any benefit over simply using (p for p in itertools.product(whatever) if condition(p)) will depend upon how many branches you can prune, because as you can see it constructs all the intermediate lists in memory.

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

Sidebar

Related Questions

According to http://docs.python.org/dev/library/plistlib.html , plistlib is available to non-Mac platforms only since 2.6, but
According to python docs http://docs.python.org/library/subprocess.html , it's recommended to replace os.popen with the Popen
Using this example from http://docs.python.org/tutorial/classes.html#class-objects : class MyClass: A simple example class i =
According to http://www.faqs.org/docs/diveintopython/fileinfo_private.html : Like most languages, Python has the concept of private elements:
In the python documentation for struct, the word buffer is used without explanation: http://docs.python.org/library/struct.html
According to AngularJS's tutorial, a controller function just sits within the global scope. http://docs.angularjs.org/tutorial/step_04
According to this http://docs.nuget.org/docs/start-here/using-the-package-manager-console I should see the PM prompt and commands like get-packages
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html According to that article STL isn't suited for game-development. What are your thoughts
According to to the SCM Activity plugin info from the Sonar Webiste ( http://docs.codehaus.org/display/SONAR/SCM+Activity+Plugin
According to http://en.wikipedia.org/wiki/Producer-consumer_problem I want to simulate P/C problem using semaphore. I am getting

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.