All combinations of a list of lists

all combinations of two lists python
combinations of items in a list
get all possible combinations of a list
all combinations of a list python
python permutations list of lists
combinations of elements in a list python
generate a list of lists which contain all subsets of l
python get all combinations of one list

I'm basically looking for a python version of Combination of List<List<int>>

Given a list of lists, I need a new list that gives all the possible combinations of items between the lists.

[[1,2,3],[4,5,6],[7,8,9,10]] -> [[1,4,7],[1,4,8],...,[3,6,10]]

The number of lists is unknown, so I need something that works for all cases. Bonus points for elegance!

you need itertools.product:

>>> import itertools
>>> a = [[1,2,3],[4,5,6],[7,8,9,10]]
>>> list(itertools.product(*a))
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 4, 10), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 5, 10), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 6, 10), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 4, 10), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 5, 10), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 6, 10), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 4, 10), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 5, 10), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 6, 10)]

Python, List comprehension can be used to convert the naive method task into a single line, hence more compact. This method checks for each element available  1. Click the Kutools > Insert > List All Combinations. 2. In the opening List All Combinations dialog box, you need to: (1) Click the Type box and select Value from the drop down list; Tip: Please select Separator from the drop down list if you need to add separator in combinations. (2) Click the Browse button to select the list of data that you will generate combinations from, and click the OK button.

The most elegant solution is to use itertools.product in python 2.6.

If you aren't using Python 2.6, the docs for itertools.product actually show an equivalent function to do the product the "manual" way:

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

How to get all unique combinations of two lists in Python, Combinations of two lists are created by pairing each element from a list with an element from another list. For example, [('a', 1), ('c', 2)] and [('b', 1),  Given two separate lists of items, how can you create a list of all possible combinations from those lists? In this post, I’ll show you how this can be done using power query. In this example I’ve set up two lists and created tables from them called List1 and List2 .

listOLists = [[1,2,3],[4,5,6],[7,8,9,10]]
for list in itertools.product(*listOLists):
  print list;

I hope you find that as elegant as I did when I first encountered it.

list of all combination from multiple lists « Python recipes , With list comprehension, you can easily loop through all combinations of 2 or 3 lists. But what about more, like 4,6, 20? This recipe show a way  1. Click Kutools > Insert > List All Combinations, see screenshot: 2. In the List All Combinations dialog box, do the following operations: (1.) Choose Value from the Type drop down list; (2.) Then click button to select the first data list that you want to use.

Numpy can do it:

 >>> import numpy
 >>> a = [[1,2,3],[4,5,6],[7,8,9,10]]
 >>> [list(x) for x in numpy.array(numpy.meshgrid(*a)).T.reshape(-1,len(a))]
[[ 1, 4, 7], [1, 5, 7], [1, 6, 7], ....]

Find all combinations of a set of lists with itertools.product, import itertools from pprint import pprint inputdata = [ ['a', 'b', 'c'], ['d'], ['e', 'f'], ] result = list(itertools.product(*inputdata)) pprint(result). Results: I thought I would add this function for those seeking an answer without importing itertools or any other extra libraries. def powerSet(items): """ Power set generator: get all possible combinations of a list’s elements Input: items is a list Output: returns 2**n combination lists one at a time using a generator Reference: 6.00.2x Lecture 2 - Decision Trees and dynamic programming

Nothing wrong with straight up recursion for this task, and if you need a version that works with strings, this might fit your needs:

combinations = []

def combine(terms, accum):
    last = (len(terms) == 1)
    n = len(terms[0])
    for i in range(n):
        item = accum + terms[0][i]
        if last:
            combine(terms[1:], item)

>>> a = [['ab','cd','ef'],['12','34','56']]
>>> combine(a, '')
>>> print(combinations)
['ab12', 'ab34', 'ab56', 'cd12', 'cd34', 'cd56', 'ef12', 'ef34', 'ef56']

Every Combination In Many Programming Languages, Dim ResultListLength = 1 For Each List In Lists ResultListLength *= List. This can be much smaller than all combinations and is used in testing where pairing  Create all possible combinations of items in a list using Linq and C#. Ask Question Asked 8 years ago. Active 8 years ago. Viewed 5k times 7. 5. I have a table of

Get all combinations of a list of lists of arrays, Get all combinations of a list of lists of arrays I have the following structure: a known number of objects, each of which contains an unknown  Generating all combinations taking one element from each list in Python can be done easily using itertools.product function. It is a part of itertools module and is very useful in this case. The short solution is as follows: list = [list1, list2] combinations = [p for p in itertools.product(*list)]

cross: Produce all combinations of list elements in purrr: Functional , A list of lists or atomic vectors. Alternatively, a data frame. cross_df() requires all elements to be named. .filter. A predicate function that takes the same number of​  The field or table below lists all of the possible combinations found by the calculator, sorted in your selected preference. If you would like to save the current entries to the secure online database, tap or click on the Data tab, select "New Data Record", give the data record a name, then tap or click the Save button.

How to get all possible combinations from multiple lists, but limited to , What I want is all possible combinations of the lists, but only output a word with a total length of the number of items in the list. For example, for the first list, these  I would like to generate all combinations of values which are in lists indexed in a dict: {'A':['D','E'],'B':['F','G','H'],'C':['I','J']} Each time, one item of each dict entry would be picked and combined to items from other keys, so I have:

  • Could someone explain the meaning of the asterisk in *a?
  • *a means these are arguments being passed to the function or method. def fn(a,b,c): would respond to fn(*[1,2,3]) reference
  • @mjallday, would it be possible to add also these combinations: (7,4,1),(8,4,1),(9,4,1),(10,4,1),(7,5,1),(8,5,1),(9,5,1),(10,5,1) etc?
  • @Reman It's not entirely clear what you want to get but if it is, for example, also the reverse of each tuple you can use a a wrapper function that takes a as input, iterates over itertools.product(*a) and yields both the tuple produced by itertools and a reverse version (e.g. create a list, reverse() it and convert back to tuple). Best ask a new question.
  • What's up with that semicolon? :)
  • Force of habit. I love how Python lets you put one semi-colon, just to help us ol' C/Java programmers. But it's clear ; is not really a statement terminator when you do something like print("foo");; which is perfectly legal in C or Java (albeit pointless) but banned in Python.
  • Could someone explain this?
  • This answer should be accepted, since it's the only one using a built-in function, while highlighting that it also works for any and also heterogenous types.
  • How is this answer any different than the one provided many years ago?