How do use itertools in Python to build permutation or combination

There is a python module dedicated to permutations and combinations called itertools.

The permutation function allows you to get permutation of N values within a list, where order matters. For instance, selecting N=2 values with [1,2,3] is done as follows:

If order is not important, you can use combinations:

Then, you may want to build all possible arrays of N values taken from a list of possible values. For instance, you may want to build a vector of length N=3 made of 0 and 1. This can be done with the cartesian product function:

This entry was posted in Python and tagged . Bookmark the permalink.

6 Responses to How do use itertools in Python to build permutation or combination

  1. Permutation_lover says:

    This python itertool is SO AMAZING!!!

  2. Pingback: Road to Data Science Week 3 Day 3 | dementopolous

  3. Zero says:

    Thanks! You save me, i need to make the closure of klenne and with this is really easy!

  4. fawwad says:

    Thanks for the blog! I was looking for it, how to generate combinations. you made it simple!

  5. Anonymous says:

    thanks

Leave a Reply

Your email address will not be published.