I'm completely new to Python and have a list of names separated by
\and, which I need to join separating the first ones with comma and the last one by 'and'. However if there are more than 4 names the return value should be the first name along with the phrase 'et al.'. So if I have
authors = 'John Bar \and Tom Foo \and Sam Foobar \and Ron Barfoo'
I should get 'John Bar et al.'. Whereas with
authors = 'John Bar \and Tom Foo \and Sam Foobar'
I should get 'John Bar, Tom Foo and Sam Foobar'.
It should also work with just one author name, returning that single name (and surname) by itself.
I tried doing something like
names = authors.split('\and') result = ', '.join(names[:-1]) + ' and '.join(names[-1])
But that obviously doesn't work. So my question is how can I use
split to get the first authors separated by comma and the last by 'and' taking into account that if there are more than four authors only the first author name should be returned along with 'et al.'.