[Python] How to join list in Python but make the last separator different?

Discussão em 'Python' iniciado por Stack, Dezembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to turn a list into separated strings joined with an ampersand if there are only two items, or commas and an ampersand between the last two e.g.

    Jones & Ben
    Jim, Jack & James


    I currently have this:

    pa = ' & '.join(listauthors[search])


    and don't know how to make sort out the comma/ampersand issue. Beginner so a full explanation would be appreciated.

    Continue reading...

Compartilhe esta Página