0%

Transforming and Combining Data · practice

Count with enumerate()

The previous lesson unpacked related values supplied by a . A sometimes needs another useful pair: the current item and its position.

We could create the position ourselves with a counter:

Try it

This works, but the counter is another we must initialize and update correctly.

Python’s enumerate() produces the position and value together:

Try it

Each loop item from enumerate is a two-item tuple. The unpacking from the previous lesson gives those items the names number and prompt.

Index or display number?

By default, enumerate(prompts) starts at 0, matching Python indexes. Compare that with display numbers that start at 1:

Try it

Choose the start based on what the number means. Use zero for an actual list . Use one for the first item in a displayed sequence.

Which loop gives you separate number and prompt values, starting number at 1?

Build numbered prompt lines

Complete a pure that returns new formatted . Let the caller select the starting number with a default.

Task

Complete number_prompts.

  • with enumerate(prompt_list, start=start).

  • Append in the form number. prompt.

  • Return the new .

  • Keep start=1 as the default.

Do not change prompt_list.