Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I get both the position and value while looping over a list?

Quick answer

In Python, unpack enumerate(items) into two loop variables: the first is the zero-based position and the second is the value. To start a displayed count at 1, use enumerate(items, start=1). That changes the counter yielded by enumerate(), not the list’s underlying positions. If you need both numbers, keep them distinct—for example, subtract 1 from a count that starts at 1 to get the corresponding zero-based position.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

How can I print each item in a Python list along with its position?

Geez

Use enumerate() in the loop and unpack each pair into a position variable and a value variable:

colors = ["red", "green", "blue"]

for index, color in enumerate(colors):
    print(index, color)

The output is:

0 red
1 green
2 blue

enumerate(sequence) yields each position together with its corresponding value, starting at position 0. [S1] The variable names are up to you; the first receives the position, and the second receives the item.

Follow-up question

I want the printed list numbers to start at 1. Does that change the list indexes?

Geez

No. Pass start=1 to make the counter yielded by enumerate() start at 1:

for number, color in enumerate(colors, start=1):
    print(number, color)

The output is:

1 red
2 green
3 blue

This changes the counter produced by enumerate(), not the list’s underlying positions. [S1] Since this loop uses the value as a reader-facing number, number is a clearer variable name than index.

Follow-up question

What if I need both the display number and the actual position in the same loop?

Geez

For a display number starting at 1, subtract 1 to get its corresponding zero-based position. For example:

items = ["notebook", "pen", "folder"]

for display_number, item in enumerate(items, start=1):
    index = display_number - 1
    print(f"display number={display_number}, index={index}, value={item}")

This prints:

display number=1, index=0, value=notebook
display number=2, index=1, value=pen
display number=3, index=2, value=folder

Here, display_number starts at 1, while index is the corresponding zero-based position. [S1] If you use a different starting number, adjust the subtraction accordingly. Alternatively, use enumerate(items) to get the index directly and calculate a display number separately.

Follow-up question

How do I decide which pattern to use, and what should I check if I get the wrong number?

Geez

Choose based on what the first loop variable represents:

# Zero-based position and value
for index, value in enumerate(items):
    print(index, value)

# Display number starting at 1 and value
for number, value in enumerate(items, start=1):
    print(number, value)

If the number will be used as a list position, use the zero-based form. If it is only for display, the second form is convenient. Check what the next part of your code expects: a display number starting at 1 and a position starting at 0 are not interchangeable. enumerate() supplies the counter and value together, so you do not need to maintain a separate counter for this pattern. [S1]

A practical checklist

  • Use enumerate(items) for a zero-based position and value.

  • Use enumerate(items, start=1) for a display number starting at 1.

  • Name the first variable index or number according to what it represents.

  • If you need both, keep the display number and zero-based position distinct.

  • Check whether later code expects a position or a display count.

A few more useful answers

Does `start=1` change the list itself?

No. It changes the counter yielded by enumerate(); it does not change the list’s items or underlying positions. [S1]

Do I need to increment a counter manually?

Not for this pattern. enumerate() yields a counter alongside each item, so you can unpack both in the loop. [S1]

What should I call the first loop variable?

Use index when it represents a zero-based position and number when it represents a display count starting at 1.

Sources

References used for this guide.

  1. [S1] Python Tutorial: Looping Techniques
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work