[Python] Is it OK to not use the value of the index 'i' inside a for loop?

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    Would it be frowned upon if the index variable i were not used inside a for loop? I have never come across a code that didn't use the value of the index while it iterates through the loop.

    def questionable():
    for i in range(3):
    print('Is this OK?') # (or do something more complicated)

    # as opposed to:

    def proper():
    for i in range(3):
    print(i) # (or do something that the value of 'i' is necessary)


    What's a more Pythonic way to rewrite the function questionable, that is, to repeatedly do something without using the iteration variable?

    Continue reading...

Compartilhe esta Página