class ListFormatter(dict):
def set(self, /, *i, a='', b=''):
self[i] = a, b
def to_string(self, x):
f = lambda: a + x[i] + b
for i, (a, b) in self.items():
for i in i: x[i] = f()
return ''.join(x)
x = ListFormatter()
match n := len(a):
case 1: ...
case 2: x.set(0, b=' and ')
case _:
x.set(*range(n - 1), b=', ')
x.set(-1, a='and ')
s = x.to_string(a)