Pythonic way to flatten lists or arbitrary depth using assignment expressions I'm working through the classic &quot;99 problems in prolog&quot; collection. But trying to do them Pythonically. Flatten a list is problem 07.
I am flattening a list of lists of arbitrary depth. I have two solutions below. Which is more Pythonic?
# helper function
def isList( x ): return isinstance( x, list )

def flatten3_h( *xs ): 
  if ( len(xs) == 0 ): return [] # base case, empty list/tuple
  hd, tl = xs[ 0 ], xs[ 1: ]
  if ( not(isList( hd ))): return [ hd ] + flatten3_h( *tl )
  return flatten3_h( *hd ) + flatten3_h( *tl )

def flatten4( *xs ):
  acc = []
  [ acc := acc + [x] if not(isList( x )) else acc + flatten4( *x ) for x in xs ]
  return acc

Here is example usage:
flatten4([ [] , [1, [2, [3, [], 4]], 5]] ) # =&gt; [1, 2, 3, 4, 5] 

I'm guessing flatten4 is more Pythonic as it uses list comprehensions.
But I'm not sure since it uses an assignment expression which I just learned about. As it's a new feature in python3.8.
Any feedback is appreciated.
Welcome to Code Review.

Follow the PEP8 guide for naming conventions. Functions and variables would be snake_case.

When checking for empty list, you don't need to find the length. Empty lists/strings/sets are all falsy values in python.

Use better/fuller names. hd/tl -&gt; head/tail.

There are a few inbuilt utilities in python, which might help you:

itertools.chain
itertools.chain.from_iterable
functools.reduce