Combinations of Pokémon
Ash, a Pokémon trainer, encounters a group of five Pokémon. These Pokémon have been loaded into a list within your session (called pokemon) and printed into the console for your convenience.
Ash would like to try to catch some of these Pokémon, but his Pokédex can only store two Pokémon at a time. Let's use combinations from the itertools module to see what the possible pairs of Pokémon are that Ash could catch.
This exercise is part of the course
Writing Efficient Python Code
Exercise instructions
- Import
combinationsfromitertools. - Create a combinations object called
combos_objthat contains all possible pairs of Pokémon from thepokemonlist. A pair has2Pokémon. - Unpack
combos_objinto a list calledcombos_2. - Ash upgraded his Pokédex so that it can now store four Pokémon. Use
combinationsto collect all possible combinations of4different Pokémon. Save these combinations directly into a list calledcombos_4using the star character (*).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import combinations from itertools
____ ____ ____ ____
# Create a combination object with pairs of Pokémon
combos_obj = ____(____, ____)
print(type(combos_obj), '\n')
# Convert combos_obj to a list by unpacking
combos_2 = ____
print(combos_2, '\n')
# Collect all possible combinations of 4 Pokémon directly into a list
combos_4 = ____
print(combos_4)