Get startedGet started for free

One-time calculation loop

A list of integers that represents each Pokémon's generation has been loaded into your session called generations. You'd like to gather the counts of each generation and determine what percentage each generation accounts for out of the total count of integers.

The below loop was written to accomplish this task:

for gen,count in gen_counts.items():
    total_count = len(generations)
    gen_percent = round(count / total_count * 100, 2)
    print(
      'generation {}: count = {:3} percentage = {}'
      .format(gen, count, gen_percent)
    )

Let's make this loop more efficient by moving a one-time calculation outside the loop.

This exercise is part of the course

Writing Efficient Python Code

View Course

Exercise instructions

  • Import Counter from the collections module.
  • Use Counter() to collect the count of each generation from the generations list. Save this as gen_counts.
  • Write a better for loop that places a one-time calculation outside (above) the loop. Use the exact same syntax as the original for loop (simply copy and paste the one-time calculation above the loop).

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import Counter
____

# Collect the count of each generation
gen_counts = ____

# Improve for loop by moving one calculation above the loop
total_count = ____

for gen,count in gen_counts.items():
    gen_percent = round(count / total_count * 100, 2)
    print('generation {}: count = {:3} percentage = {}'
          .format(gen, count, gen_percent))
Edit and Run Code