Python script that flattens multidimensional arrays