I so rarely need to save a list in python in a form acceptable to LISP but here is a go with no visible recursion needed.
>>> nested = [1, 2, [3, 4, [5, 6, 7], 8], 9] >>> print(nested) [1, 2, [3, 4, [5, 6, 7], 8], 9] # Just converting to a tuple does not change nested lists >>> print(tuple(nested)) (1, 2, [3, 4, [5, 6, 7], 8], 9) # But a function that typographically replaces [] with () needs no recursion >>> def p2b(nested_list): return >>> repr(nested_list).replace('[','(').replace(']',')') >>> print(p2b(nested)) (1, 2, (3, 4, (5, 6, 7), 8), 9) People who speak python well do not necessarily lisp. -----Original Message----- From: Python-list <python-list-bounces+avi.e.gross=gmail....@python.org> On Behalf Of Hen Hanna Sent: Sunday, February 26, 2023 4:54 AM To: python-list@python.org Subject: Re: one Liner: Lisprint(x) --> (a, b, c) instead of ['a', 'b', 'c'] On Saturday, February 25, 2023 at 11:45:12 PM UTC-8, Hen Hanna wrote: > def Lisprint(x): print( ' (' + ', '.join(x) + ')' , '\n') > > a= ' a b c ? def f x if zero? x 0 1 ' > a += ' A B C ! just an example ' > x= a.split() > > print(x) > Lisprint(x) > > ['a', 'b', 'c', '?', 'def', 'f', 'x', 'if', 'zero?', 'x', '0', '1', 'A', 'B', > 'C', '!', 'just', 'an', 'example'] > > (a, b, c, ?, def, f, x, if, zero?, x, 0, 1, A, B, C, !, just, an, example) For nested lists.... impossible to improve upon P.Norvig's code def Lisprint(x): print(lispstr(x)) def lispstr(exp): "Convert a Python object back into a Lisp-readable string." if isinstance(exp, list): return '(' + ' '.join(map(lispstr, exp)) + ')' else: return str(exp) a= ' a b c ' x= a.split() x += [['a', 'b', 'c']] x += x print(x) Lisprint(x) ['a', 'b', 'c', ['a', 'b', 'c'], 'a', 'b', 'c', ['a', 'b', 'c']] (a b c (a b c) a b c (a b c)) ---------- Without the commas, the visual difference (concision) is striking ! -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list