How to join list in Python but make the last separator different?
Solution 1:
"&".join([",".join(my_list[:-1]),my_list[-1]])
I would think would work
or maybe just
",".join(my_list[:-1]) +"&"+my_list[-1]
to handle edge cases where only 2 items you could
"&".join([",".join(my_list[:-1]),my_list[-1]] if len(my_list) > 2 else my_list)
Solution 2:
You could break this up into two joins. Join all but the last item with ", "
. Then join this string and the last item with " & "
.
all_but_last = ', '.join(authors[:-1])
last = authors[-1]
' & '.join([all_but_last, last])
Note: This doesn't deal with edge cases, such as when authors
is empty or has only one element.