Python – Find Combinations – Itertools.combinations

The question starts with how to find all the possible combinations for the elements in a given list. And I found the Python solution here. Of course, Python has already some built-in function to take care of it. We have to use the combinations function from library itertools to do it.

However, combinations function will only be a proxy as the C(3,n), so it will only return an interator with all the combinations that for a given length. To find all the combinations of the a set, we need to loop through all the possible length and add the possible combinations to the result.

python_itertools_combinations

There are also a few interesting functions in itertools like permuations and group_by etc, which are all super useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s