[Python] How best to insert a separator between list elements (but not at the beginning or...

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

  1. Stack

    Stack Membro Participativo

    I'm a beginner, practicing Python, I want to understand if one or the other way is better version. If yes, please elaborate! And how to determine the same in future, on my own?

    words = ['This', 'is', 'a', 'list']
    separator = ' - '

    for word in words:
    if words.index(word) < len(words) - 1:
    print(word+separator, end='')
    else:
    print(word,end='')
    print()


    OR

    words = ['This', 'is', 'a', 'list']

    for word in words:
    if words.index(word) < len(words) - 1:
    print(word, end=' - ')
    else:
    print(word, end=' ')
    print()

    Continue reading...

Compartilhe esta Página