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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:11:40+00:00 2026-06-13T19:11:40+00:00

What is the pythonic way to calculated all the product combinations of two lists.

  • 0

What is the pythonic way to calculated all the product combinations of two lists. So given two lists of length n I’d liked to return a list of length 2^n containing the products.

Like list(itertools.product(on,off)) but the results should use all four elements not only combination pairs like:

[(1.05, 5.53), (1.05, 3.12), (1.05, 3.75), (1.05, 4.75), (1.5, 5.53), (1.5, 3.12), (1.5, 3.75), (1.5, 4.75), (2.1, 5.53), (2.1, 3.12), (2.1, 3.75), (2.1, 4.75), (1.7, 5.53), (1.7, 3.12), (1.7, 3.75), (1.7, 4.75)]

So more like this:

off = [5.53,3.12,3.75,4.75]
on = [1.05,1.5,2.1,1.7]

# calculate combinations
x = combinations(on,off)

# Where... 
# x[0] = off[0] * off[1] * off[2] * off[3] i.e
# x[0] = 5.53 * 3.12 * 3.75 * 4.75
#
# x[1] = off[0] * off[1] * off[2] * on[3] i.e
# x[1] = 5.53 * 3.12 * 3.75 * 1.7
#
# x[2] = off[0] * off[1] * on[2] * on[3] i.e
# x[2] = 5.53 * 3.12 * 2.1 * 1.7
#
# ...
#
# x[15] = on[0] * on[1] * on[2] * on[3] i.e
# x[15] = 1.05 * 1.5 * 2.1 * 1.7

The output can be similar to the itertools.product() method i.e [(5.53, 3.12, 3.75, 4.75),(5.53, 3.12, 3.75, 1.7), ...] I need to calculate the product but I’m interesting in the combination method.

Note: When I say pythonic way of doing this I mean a simple one or two lines taking advantage of pythons structures, libraries (itertools ect).

  • 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-13T19:11:41+00:00Added an answer on June 13, 2026 at 7:11 pm

    You can generate all 2**4 possibilities by starting with itertools.product([0, 1], 4). This enumerates all possible sequences of 0s and 1s of length 4, and then you can translate each 0-1 sequence to a sequence of values from off and on, by taking off[i] if the ith element of the 0-1 sequence is 0, and on[i] otherwise. In code:

    >>> import itertools
    >>> off = [5.53,3.12,3.75,4.75]
    >>> on = [1.05,1.5,2.1,1.7]
    >>> for choices in itertools.product([0, 1], repeat=len(off)):
    ...     print [(on[i] if choice else off[i]) for i, choice in enumerate(choices)]
    ... 
    [5.53, 3.12, 3.75, 4.75]
    [5.53, 3.12, 3.75, 1.7]
    [5.53, 3.12, 2.1, 4.75]
    [5.53, 3.12, 2.1, 1.7]
    ... <10 more entries omitted ...>
    [1.05, 1.5, 2.1, 4.75]
    [1.05, 1.5, 2.1, 1.7]
    

    To print the products instead of the lists:

    >>> import operator
    >>> for choices in itertools.product([0, 1], repeat=len(off)):
    ...     elts = [(on[i] if choice else off[i]) for i, choice in enumerate(choices)]
    ...     print reduce(operator.mul, elts, 1)
    ... 
    307.32975
    109.9917
    172.10466
    61.595352
    ...
    

    If you have numpy available and are willing to work with numpy arrays instead of Python lists, then there are some nice tools like numpy.choose available. For example:

    >>> import numpy
    >>> numpy.choose([0, 1, 0, 1], [off, on])
    array([ 5.53,  1.5 ,  3.75,  1.7 ])
    >>> numpy.product(numpy.choose([0, 1, 0, 1], [off, on]))
    52.880624999999995
    

    Combining with the earlier solutions gives:

    >>> for c in itertools.product([0, 1], repeat=4):
    ...     print numpy.product(numpy.choose(c, [off, on]))
    ... 
    307.32975
    109.9917
    172.10466
    61.595352
    147.7546875
    52.880625
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is a pythonic way of making list of arbitrary length containing evenly spaced
What is the pythonic way of iterating simultaneously over two lists? Suppose I want
What is the pythonic way of doing the following: I have two lists a
There must be a simpler, more pythonic way of doing this. Given this list
Possible Duplicate: pythonic way to sort a list of lists by the last item
I'm looking for a fast, clean, pythonic way to divide a list into exactly
Which is the most pythonic way to convert a list of tuples to string?
I am looking for the most pythonic way of splitting a list of numbers
Is there a pythonic way to build up a list that contains a running
What is the most pythonic way to truncate a list to N indices when

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.