Python – Permutations and Combinations Made Easy with Itertools

Long time ago, various people, including me, were trying to make permutations and combinations with nested loops and various other “heavy” structures. Anyway, in Python there is a library, which does this for us and sets the end of the useless nested loops or nested loops with recursion. The library is quite understandable and is called itertools

In this article, I will try to present a few “magic features” of itertools, as my own go-to place, if I have to do it again.

All possible combinations there…

Given the list(1,2,3,4), you may like to see all lists of n elements with the elements of this list. If is 3, then this is the result of your expectations:

The trick is that can be bigger than the list, and then the results become quite a lot… E.g. with n = 7 we get 16384 different lists, starting from (1,1,1,1,1,1,1) and finishing with (4,4,4,4,4,4,4). The good thing is that you do not need to take care of it and you would not get crazy writing nested loops with recursion. It is that easy with Python:

Only combinations with non-repeatable (unique) elements, where order matters

If we want to have all the 3 digit lists from (1, 2, 3, 4), where the elements are not to be repeated, it is quite ok as well – itertools.permutations((1, 2, 3, 4), 3) does the magic for us:

resulting in:

Combinations where order does not matter – like in the lottery

The third thing to be presented here is a set of combinations, in which the order does not matter. It is like in the lottery – you “only” need to guess 6 numbers correctly and their order is not relevant. In our case, all the combinations of 3 elements from a list with 4 numbers are the following:

Yup, just 4. Got with the following code:

If you are interested to see the total number of  combinations from 6/49 lottery and you cannot calculate the following factorials:

nor you are interesting in searching in google for the answer, then you may run the code below:

It returns exactly 13983816, which is the count of the combinations of the lotto 6/49. Almost 14 million!

🙂

Tagged with: , , , ,