Holistic conversion loop
A list of all possible Pokémon types has been loaded into your session as pokemon_types. It's been printed in the console for convenience.
You'd like to gather all the possible pairs of Pokémon types. You want to store each of these pairs in an individual list with an enumerated index as the first element of each list. This allows you to see the total number of possible pairs and provides an indexed label for each pair.
The below loop was written to accomplish this task:
enumerated_pairs = []
for i,pair in enumerate(possible_pairs, 1):
enumerated_pair_tuple = (i,) + pair
enumerated_pair_list = list(enumerated_pair_tuple)
enumerated_pairs.append(enumerated_pair_list)
Let's make this loop more efficient using a holistic conversion.
Deze oefening maakt deel uit van de cursus
Writing Efficient Python Code
Oefeninstructies
combinationsfrom theitertoolsmodule has been loaded into your session. Use it to create a list calledpossible_pairsthat contains all possible pairs of Pokémon types (each pair has2Pokémon types).- Create an empty list called
enumerated_tuplesabove the for loop. - Use a built-in function to convert each tuple in
enumerated_tuplesto a list.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Collect all possible pairs using combinations()
possible_pairs = [*____(pokemon_types, ____)]
# Create an empty list called enumerated_tuples
____ = ____
for i,pair in enumerate(possible_pairs, 1):
enumerated_pair_tuple = (i,) + pair
enumerated_tuples.append(enumerated_pair_tuple)
# Convert all tuples in enumerated_tuples to a list
enumerated_pairs = [*____(____, enumerated_tuples)]
print(enumerated_pairs)