How do i get a list of combinations in python?

I'm a bit late on this topic, but think I can help someone.

You can use product from itertools:

from itertools import product

n = [1, 2, 3]

result = product(n, repeat=3) # You can change the repeat more then n length

print(list(result))

Output:

[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1),
 (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2),
 (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), 
(3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

Another example, but changing repeat arguement:

from itertools import product

n = [1, 2, 3]

result = product(n, repeat=4) # Changing repeat to 4
print(list(result))

Output:

(1, 1, 2, 3), (1, 1, 3, 1), (1, 1, 3, 2), (1, 1, 3, 3), (1, 2, 1, 1), 
(1, 2, 1, 2), (1, 2, 1, 3), (1, 2, 2, 1), (1, 2, 2, 2), (1, 2, 2, 3), 
(1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 3, 1, 1), (1, 3, 1, 2), 
(1, 3, 1, 3), (1, 3, 2, 1), (1, 3, 2, 2), (1, 3, 2, 3), (1, 3, 3, 1), 
(1, 3, 3, 2), (1, 3, 3, 3), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 1, 3), 
(2, 1, 2, 1), (2, 1, 2, 2), (2, 1, 2, 3), (2, 1, 3, 1), (2, 1, 3, 2),
 (2, 1, 3, 3), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 1, 3), (2, 2, 2, 1), 
(2, 2, 2, 2), (2, 2, 2, 3), (2, 2, 3, 1), (2, 2, 3, 2), (2, 2, 3, 3), 
(2, 3, 1, 1), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 2, 1), (2, 3, 2, 2), 
(2, 3, 2, 3), (2, 3, 3, 1), (2, 3, 3, 2), (2, 3, 3, 3), (3, 1, 1, 1), 
(3, 1, 1, 2), (3, 1, 1, 3), (3, 1, 2, 1), (3, 1, 2, 2), (3, 1, 2, 3), 
(3, 1, 3, 1), (3, 1, 3, 2), (3, 1, 3, 3), (3, 2, 1, 1), (3, 2, 1, 2), 
(3, 2, 1, 3), (3, 2, 2, 1), (3, 2, 2, 2), (3, 2, 2, 3), (3, 2, 3, 1), 
(3, 2, 3, 2), (3, 2, 3, 3), (3, 3, 1, 1), (3, 3, 1, 2), (3, 3, 1, 3), 
(3, 3, 2, 1), (3, 3, 2, 2), (3, 3, 2, 3), (3, 3, 3, 1), (3, 3, 3, 2), 
(3, 3, 3, 3)]```

In this tutorial, you’ll learn how to use Python to get all combinations of a list. In particular, you’ll learn how to how to use the itertool.combinations method to generate a list of all combinations of values in a list.

The Quick Answer: Use itertools.combinations to Get All Combinations of a List

How do i get a list of combinations in python?

  • What Does it Mean to Get All Combinations of a List?
  • How to Use Itertools to Get All Combinations of a List in Python
  • How to Get All Combinations of Unique Values of a List in Python
  • How to Get All Combinations with Replacement of a List in Python
  • Conclusion

What Does it Mean to Get All Combinations of a List?

In your Python journey, you may encounter the need to get all combinations of the items in a list. But what does this mean?

Let’s say you have a list that looks like this: ['a', 'b', 'c'].

When you create a list of all possible combinations, you’ll end up with a list that looks like this: [(), ('a',), ('b',), ('c',), ('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c')]. Here we get a list of tuples that contain all possible combinations without replacement.

Now that you know what it means to get a list of all possible combinations of a list in Python, let’s see how you can get this done in Python!

Python comes built-in with a helpful library called itertools, that provides helpful functions to work with iteratable objects. One of the many functions it comes with it the combinations() function. This, as the name implies, provides ways to generate combinations of lists.

Let’s take a look at how the combinations() function works:

itertools.combinations(iterable, r)
  • iterable refers to the iterable for which you want to find combinations,
  • r refers to the length of the combinations you want to produce

Now that you know how the combinations() function works, let’s see how we can generate all possible combinations of a Python list’s items:

from itertools import combinations

sample_list = ['a', 'b', 'c']
list_combinations = list()

for n in range(len(sample_list) + 1):
    list_combinations += list(combinations(sample_list, n))

print(list_combinations)

# Returns: [(), ('a',), ('b',), ('c',), ('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c')]

Let’s break down what we’ve done here:

  1. We import the combinations function from itertools
  2. We create a sample list and an empty list to store our data in
  3. We then create a for-loop to loop over all possible combinations of lengths. To make this dynamic, we use the range() function, since we may not know how long our list is at any given time.
  4. We then create a list out of the combinations object that’s returned from passing in our sample list and our n parameter

We can see that our list includes a blank combination as well. If we wanted to omit this, we could change our for-loop to be from range(1, len(sample_list)+1), in order to have a minimum number of elements in our combination.

In the next section, you’ll learn how to get all combinations of only unique values in a list.

Want to learn more about Python for-loops? Check out my in-depth tutorial here to learn all you need to know!

How to Get All Combinations of Unique Values of a List in Python

In this section, you’ll learn how to get all combinations of only unique values of a list in Python. Since Python lists can contain duplicate values, we’ll need to figure out how to do this.

Say we have a list that looks like this: ['a', 'b', 'c', 'c']. Instead of including duplicate combinations of c in our final list, we’ll first need to remove duplicates from our list.

Let’s see how we can do this in Python:

from itertools import combinations

sample_list = ['a', 'b', 'c', 'c']
list_combinations = list()

sample_set = set(sample_list)
for n in range(len(sample_set) + 1):
    list_combinations += list(combinations(sample_set, n))

print(list_combinations)

This follows the same logic as the example above. The only difference is that we have first created a set out of our list. Sets are a unique data structure in Python that require each item to be unique. Therefore, it’s a helpful way to de-duplicate our list.

We then iterate over the length of the set and the set itself, to create all possible combinations.

How to Get All Combinations with Replacement of a List in Python

In this final section, you’ll learn how to get all combinations of a list in Python with replacements. Meaning, that a single element has the potential for being picked again.

Let’s see how this can be done in Python, using itertools and the combinations_with_replacement function. The function does exactly what it is described as: it gets the combinates with replacements.

from itertools import combinations_with_replacement

sample_list = ['a', 'b', 'c']
list_combinations = list()

for n in range(len(sample_list) + 1):
    list_combinations += list(combinations_with_replacement(sample_list, n))

print(list_combinations)

# Returns: [(), ('a',), ('b',), ('c',), ('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c'), ('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'c', 'c'), ('b', 'b', 'b'), ('b', 'b', 'c'), ('b', 'c', 'c'), ('c', 'c', 'c')]

We can see here that each item has the potential for being included once, twice, or three times in a list of three items.

Conclusion

In this post, you learned how to get all combinations of a list in Python. You learned how to do this with the itertools.combinations function and the `itertools.combinations_with_replacement_ function. The functions allow you to pass in a list and get the combinations without and with replacements, respectively.

To learn more about the itertools.combinations function, check out the official documentation here.

How do you generate all possible combinations of one list?

To list all combinations possible in an Excel sheet, follow the following procedure;.
Step 1: Open the sheet. You first need to open the sheet with data from which you want to make all possible combinations. ... .
Step 2: Select cell for result. ... .
Step 3: Drag the formula to other cells..

How do you generate all possible combinations of two lists in Python?

How to get all unique combinations of two lists in Python.
list1 = ["a", "b", "c"].
list2 = [1, 2].
all_combinations = [].
list1_permutations = itertools. permutations(list1, len(list2)) ... .
for each_permutation in list1_permutations:.
zipped = zip(each_permutation, list2).
all_combinations. ... .
print(all_combinations).

How do you print all combinations in Python?

Program.
def comb(L):.
a=int(input("Enter first number:")).
b=int(input("Enter second number:")).
c=int(input("Enter third number:")).
L.append(a).
L.append(b).
L.append(c).
for i in range(3):.

How do you get all the combinations of a string in Python?

Find all permutations of a string in Python.
import itertools..
if __name__ == '__main__':.
s = 'ABC'.
nums = list(s).
permutations = list(itertools. permutations(nums)).
# Output: ['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA'].
print([''. join(permutation) for permutation in permutations]).