[Python] How do I get both the index and value when looping through a Python list? [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to loop over a Python list and print each item along with its index, but my current code only gives me the values—how can I also get the index?

    Here’s what I tried:

    names = ["Alice", "Bob", "Charlie"]
    for name in names:
    print(name)


    This outputs:

    Alice
    Bob
    Charlie


    But I want:

    0 Alice
    1 Bob
    2 Charlie


    What’s the best, most Pythonic way to do this?

    Continue reading...

Compartilhe esta Página