make all possible combinations of lists


Dean:

I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions that are used, itertoolsbut when I only need one these functions return objects list.

Oscar López:

Just use . E.g:itertools.combinations

import itertools

lst = [1, 2, 3]
combs = []

for i in xrange(1, len(lst)+1):
    combs.append(i)
    els = [list(x) for x in itertools.combinations(lst, i)]
    combs.append(els)

Keep this value for now :combs

[1, [[1], [2], [3]], 2, [[1, 2], [1, 3], [2, 3]], 3, [[1, 2, 3]]]

Yes, it's slightly different from the example output you provided, but in that output you don't list all possible combinations.

I listed the combined sizes before the actual list of each size, if all you need is the combined (no size, as in the example output), then try a different version of the code:

import itertools

lst = [1, 2, 3]
combs = []

for i in xrange(1, len(lst)+1):
    els = [list(x) for x in itertools.combinations(lst, i)]
    combs.extend(els)

Keep this value for now :combs

[[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

Related


make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of vector lists

citizen I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time. For example, I have the following list: > DF $`1` A B

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of vector lists

citizen I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time. For example, I have the following list: > DF $`1` A B

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

Find all possible combinations of lists in VBA

username I'm trying to organize a list of data "a,b,c,d,e,...." into all possible combinations of 1,2,3....n elements. E.g: a,b,c,d,e a b c d e a,b a,c a,d a,e b,c b,d d,e ... etc. So far I've only met people who wrote code that found a combination of two set

count of all possible combinations from multiple lists

JDraper I have the following list: list_a = set(["A", "B", "C", "D", "E", "F"]) list_b = set(["1", "2", "3", "4", "5", "6"]) list_c = set(["red", "yellow", "blue", "green"]) I want to find the total number of possible combinations of these lists (one item p

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

count of all possible combinations from multiple lists

JDraper I have the following list: list_a = set(["A", "B", "C", "D", "E", "F"]) list_b = set(["1", "2", "3", "4", "5", "6"]) list_c = set(["red", "yellow", "blue", "green"]) I want to find the total number of possible combinations of these lists (one item p

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

all possible combinations of items in n lists

Bernat Ibanez I need to develop a list that contains all possible combinations in order of elements in n lists. Basically, I'm trying to find all possible paths that will be needed later in another part of the program. I've written some simple code for the two

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Python 3.3: all possible combinations of lists

Michal I have a list like this: [['one', 'two', 'three', ...], ['a', 'b', ...], ['left', 'right'] ...] and I need to create all possible combinations of this item and put it into a string like: "one|a|left" "one|a|right" "one|b|left" "one|b|right" "two|a|left

Find all possible combinations of lists in VBA

username I'm trying to organize a list of data "a,b,c,d,e,...." into all possible combinations of 1,2,3....n elements. E.g: a,b,c,d,e a b c d e a,b a,c a,d a,e b,c b,d d,e ... etc. So far I've only met people who wrote code that found a combination of two set

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of