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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:21:50+00:00 2026-05-13T23:21:50+00:00

This question asks how to compute the Cartesian product of a given number of

  • 0

This question asks how to compute the Cartesian product of a given number of vectors. Since the number of vectors is known in advance and rather small, the solution is easily obtained with nested for loops.

Now suppose that you are given, in your language of choice, a vector of vectors (or list of lists, or set of sets, etc.):

l = [ [1,2,3], [4,5], [6,7], [8,9,10], [11,12], [13] ]

If I was asked to compute its Cartesian product, that is

[ [1,4,6,8,11,13], [1,4,6,8,12,13], [1,4,6,9,11,13], [1,4,6,9,12,13], ... ]

I would proceed with recursion. For example, in quick&dirty python,

def cartesianProduct(aListOfLists):
    if not aListOfLists:
        yield []
    else:
        for item in aListOfLists[0]:
            for product in cartesianProduct(aListOfLists[1:]):
                yield [item] + product

Is there an easy way to compute it iteratively?

(Note: The answer doesn’t need to be in python, and anyway I’m aware that in python itertools does the job better, as in this question.)

  • 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-13T23:21:50+00:00Added an answer on May 13, 2026 at 11:21 pm

    1) Create a list of indexes into the respective lists, initialized to 0, i.e:

    indexes = [0,0,0,0,0,0]
    

    2) Yield the appropriate element from each list (in this case the first).

    3) Increase the last index by one.

    4) If the last index equals the length of the last list, reset it to zero and carry one. Repeat this until there is no carry.

    5) Go back to step 2 until the indexes wrap back to [0,0,0,0,0,0]

    It’s similar to how counting works, except the base for each digit can be different.


    Here’s an implementation of the above algorithm in Python:

    def cartesian_product(aListOfList):
        indexes = [0] * len(aListOfList)
        while True:
            yield [l[i] for l,i in zip(aListOfList, indexes)]
            j = len(indexes) - 1
            while True:
                indexes[j] += 1
                if indexes[j] < len(aListOfList[j]): break
                indexes[j] = 0
                j -= 1
                if j < 0: return
    

    Here is another way to implement it using modulo tricks:

    def cartesian_product(aListOfList):
        i = 0
        while True:
            result = []
            j = i
            for l in aListOfList:
                 result.append(l[j % len(l)])
                 j /= len(l)
            if j > 0: return
            yield result
            i += 1
    

    Note that this outputs the results in a slightly different order than in your example. This can be fixed by iterating over the lists in reverse order.

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

Sidebar

Related Questions

This question has been asked before but the accepted solution (given by the question
In this question superwiren asks about the pitfalls for converting a .net 2.0 solution
This question asks the general question. I'm asking about VHDL in particular, since the
This question asks about storing a single graph in a relational database. The solution
This question asks about file name the servers sends and the fix was quite
This post asks this question but doesn't really give an answer, so I thought
This other SO question asks about an autocomplete textbox in WPF. Several people have
This question asks about getting a random(ish) sample of records on SQL Server and
This question asks how to retrieve the handle of the DLL that contains the
This question asks how to setup your path variable in Windows to include the

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.