I looking over some old code, I had written the function below with a C-style for-loop iterating over a count variable. I then fixed it up with new knowledge, but still am suspicious that there is even a better way to do this.
The function should behave like so:
>>> join_list(['a']) 'a' >>> join_list(['a', 'b']) 'a or b' >>> join_list(['a', 'b', 'c']) 'a, b, or c'
The function I have currently:
def join_list(my_list): length = len(my_list) if length == 1: return my_list elif length == 2: return ' or '.join(my_list) else: head = ', '.join(my_list[:-1]) return head + ', or ' + my_list[-1]