Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #359 Create a formatted list

Create the end-user text, s, specifying the contents of list a.

For example,
– "A", "A and B", or "A, B, and C"
– "1", "1 or 2", or "1, 2, or 3"

def f(a, sep=', ', con=' or '):
    *x, s = map(str, a)
    return sep.join(x) + con + s
*x, s = map(str, a)
if x: s = ', '.join(x) + f' and {s}'
SysUtils
  if Length(a) > 1 then
  begin
    s := String.Join(', ', a, 0, Length(a)-1);
    s := s + ' or ' + a[High(a)]
  end
  else
  begin
    if Length(a) > 0 then
      s := a[0]
    else
      s :='';
  end;

New implementation...
< >
reilas