STEM

Combinations in Python

In this post, I present the concept of calculating combinations from different choices. For educational fun, I will combine various parts of the bodies from two different creatures and see how many combinations we get. You’ll get to enter the animals to combine, and even get a name for your new mutant!


If you have ever played Impossible Creatures the game published by Microsoft back in 2003, it might tease some nostalgia. If not, no worries, the concept of combinations is applicable in many areas of life and is quite valuable. Let’s start with a simple example.

If you have a choice to pick the head of an animal from two different animals, how many combinations of the creature could you make? Easy, right? Two! One combination would have a head from animal #1, another from animal # 2. Now, what if you could switch the head, front legs, body, hind legs, and tail from one to the other freely? How many combinations of mutants could you produce? The answer is 32. That’s because: 2 choices (head) × 2 choices (body) × 2 choices (front legs) × 2 choices (hind legs) × 2 choices (tail) = 32 total combinations (that includes all of animal #1, and all of animal # 2 as-is…so there’s the 2 constants plus 30 new combinations involving both animals).

That was exactly the premise of the game Impossible Creatures. By combining different parts, and each animal’s part having unique abilities (strengths and weaknesses), you could create a new creature with new capabilities that doesn’t exist in nature. In this post however, I’m not going to delve into the advanced graphics, rather the implementation of this combination concept in Python using simple text (you’re free to extend it to graphic engines).

The runnable code below will allow you to enter names of animal #1 and # 2. Then it’ll show all combinations in detail, additionally, the code will also create a new, unique name based on the animals you combined. Click on Run in the widget below to run the code and see for yourself.

What’s happening in the code?

Here, I’ll provide an overview of how it’s made possible in Python. I create dictionary objects for each animal with body parts that are switchable. These are the keys in the dictionaries. The values of the keys are dynamically filled based on the user-inputs for the animal names. Then the script uses itertools.product() function to generate all possible combinations by swapping the parts of the two animals. Each unique combination is displayed in a readable text format in the ouput when your run the code, followed by a unique name of the combined creature.

To get the full source code, become my Patreon using the link in the section below.

I hope you found this post helpful and interesting. Explore this site for more tips and articles. Be sure to also check out my Patreon site where you can find free downloads and optional fee-based code and documentation. Thanks for visiting!

I hope it was fun and interesting. Keep exploring!

Back To Top