List values are a rather esoteric property of python and some other languages. This can confuse many people. However, the structure that they follow is incredibly simple and based on establishing a notation :
- A variable representing the elements of the input list in this case calls a recursive function call.
- List to work in this case n
- Optional predicate expression.
- An output expression that creates entries in the output list based on entries in the input list that satisfy the predicate.
The function will return the largest list / subscription size when n entered. Understanding the list contained in the max function call creates a list of all sizes of values ββin the list (0 if it is not a list), using the recursive call on its own to traverse the sub-lists in the same way.
To help explain the code, you are writing another function to return the input array passed to the max function call to improve your understanding:
def b(n): return [len(n)]+[b(i) for i in n] if isinstance(n,list) else 0
Example:
>>> x = [1,2,3,4,5,[1,2,3,4,5,6,7,8,9],7,[1,2,[1,2,3,4,5,6,7,8,9,10,11,12,13,14],4,5,6,[1,2]]] >>> a(x) 14 >>> b(x) [8, 0, 0, 0, 0, 0, [9, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0, [7, 0, 0, [14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0, 0, 0, [2, 0, 0]]]
NB In response to your change, the + operator allows you to combine the two lists together. eg:.
x = [a,b,c] y = [d,e,f] z = x + y;