Get all combinations from a list of lists (combination algorithm)


Robert Tiger

Say I have this list of lists:

lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']]

How do I iterate to output all my possible combinationslen(lst_of_lsts)

Desired output:

comb1 = ['1-1', '2-1', '3-1']
comb2 = ['1-2', '2-1', '3-1']
comb3 = ['1-3', '2-1', '3-1']
comb4 = ['1-1', '2-2', '3-1']
comb5 = ['1-2', '2-2', '3-1']
comb6 = ['1-3', '2-2', '3-1']
comb7 = ['1-1', '2-3', '3-1']
comb8 = ['1-2', '2-3', '3-1']
comb9 = ['1-3', '2-3', '3-1']
comb10 = ['1-1', '2-1', '3-2']
comb11 = ['1-1', '2-1', '3-3']
comb12 = ['1-1', '2-2', '3-2']

wait wait wait...

you understood..

Jan Pöschko

I think what you want is not technically a combined list (order doesn't matter), but a product of a given list , i.e. all lists where the first element is from the first list and the second is from the second list ,So on and so forth.

This (and many other useful related functions) are available in the itertools library , especially itertools.product :

>>> from itertools import product
>>> list(product(*lst_of_lsts))
[('1-1', '2-1', '3-1'), ('1-1', '2-1', '3-2'), ('1-1', '2-1', '3-3'), ('1-1', '2-2', '3-1'), ('1-1', '2-2', '3-2'), ('1-1', '2-2', '3-3'), ('1-1', '2-3', '3-1'), ('1-1', '2-3', '3-2'), ('1-1', '2-3', '3-3'), ('1-2', '2-1', '3-1'), ('1-2', '2-1', '3-2'), ('1-2', '2-1', '3-3'), ('1-2', '2-2', '3-1'), ('1-2', '2-2', '3-2'), ('1-2', '2-2', '3-3'), ('1-2', '2-3', '3-1'), ('1-2', '2-3', '3-2'), ('1-2', '2-3', '3-3'), ('1-3', '2-1', '3-1'), ('1-3', '2-1', '3-2'), ('1-3', '2-1', '3-3'), ('1-3', '2-2', '3-1'), ('1-3', '2-2', '3-2'), ('1-3', '2-2', '3-3'), ('1-3', '2-3', '3-1'), ('1-3', '2-3', '3-2'), ('1-3', '2-3', '3-3')]

Note that using *since productexpects the outer list as a positional argument, and using list(for better illustration) because (as productrequired) returns an iterator.

Related


Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

How to get all combinations of "sub"lists from a list

Wow So if I start with: x = [a, b, c] y = [[a], [b], [c], [a,b], [b,c]] How can I get all combinations xof elements from it y? It's just like: y = [ [[a, b], c], [[b,c], a], [a, b, c] I've looked into itertools and list comprehensions but still struggling. A

How to get all combinations of "sub"lists from a list

Wow So if I start with: x = [a, b, c] y = [[a], [b], [c], [a,b], [b,c]] How can I get all combinations xof elements from it y? It's just like: y = [ [[a, b], c], [[b,c], a], [a, b, c] I've looked into itertools and list comprehensions but still struggling. A

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

Create all combinations of lists from a list of lists in python

crepes I have a list:l = [1, (2, 3), (4, 5), 6] I want to select 3 items from this list, but only one item per sublist. Using combinationsI can achieve: Result = [[1, (2, 3), (4, 5)], [1, (2, 3), 6], [1, (4, 5), 6]] However, tuples do not expand. What's the mo

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now I'm trying to write a function that takes an array and an integer n and gives a list of each combination of size n (i.e. a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I don'

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now, I'm trying to write a function that takes an array and an integer n, and gives a list of each combination of size n (hence a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I c