0

I have a list of arrays as follows:

list = [
         array([4.1,3.2,5.2]),
         array([4.1,8.5]),
         array([1.2,4.5,7.4])
        ]

I want to convert the above list into a single list as below:

list = [4.1,3.2,5.2,4.1,8.5,1.2,4.5,7.4]

I want to do the above thing in python.

EDIT:

The code I used to generate the list is

  result = []
  result.append(mfccs_processed)
  result.append(chroma_processed)
  result.append(mel_processed)

Now result is showing as follows:

[array([-445.43651286,  111.90097184,  -21.00590592,   31.34361885,
      9.39596702,   -4.40772227,    2.3648624 ,   -1.68017743,
     -6.39975114,   -1.12694295,   -9.34500736,   -1.13545822,
     -7.88168904,  -10.9743485 ,   -2.77344424,   -1.23599528,
     -7.39119564,   -2.44394294,   -3.75691633,   -4.80426688,
     -1.94571499,   -3.95747011,   -5.72427054,   -4.81378321,
     -3.70200738,   -5.19167481,   -4.47207894,   -4.2216418 ,
     -4.1305041 ,   -5.10309738,   -3.89591536,   -2.67058964,
     -4.04656319,   -4.78655754,   -2.44667325,   -3.05293339,
     -4.17915009,   -2.90706455,   -2.8132492 ]), array([0.548945  , 0.59981299, 0.58249648, 0.5834056 , 0.62918682,
   0.58651369, 0.51776701, 0.5152288 , 0.56039473, 0.5606346 ,
   0.59585897, 0.56108877]), array([5.36600691e-05, 3.20399714e-04, 1.19905308e-03, 3.57715592e-03,
   5.45226355e-03, 7.65769149e-02, 9.67304585e-01, 1.37048258e+00,
   2.20317190e+00, 1.47716846e+00, 7.10145651e-01, 7.23264577e-02,
   3.20378431e-02, 2.13741716e-01, 3.50276892e-01, 3.25545211e-01,
   8.23071739e-01, 1.10485735e+00, 5.16918943e-01, 8.84181377e-01,
   9.06340752e-01, 3.95779410e-01, 3.13891357e-01, 1.13048804e-01,
   6.61881599e-01, 1.70450985e+00, 1.82170031e+00, 6.18310782e-01,
   4.05325714e-01, 5.89335840e-01, 4.31023403e-01, 9.29283876e-02,
   4.11129880e-02, 1.71606268e-01, 1.41923368e-01, 9.29835332e-02,
   1.45740019e-01, 1.27688893e-01, 2.34819748e-02, 9.72517361e-03,
   3.63473003e-02, 5.93138929e-02, 3.11671706e-02, 1.18442648e-02,
   8.33423898e-03, 1.87190944e-02, 3.29460414e-02, 2.69168857e-02,
   9.29586223e-03, 6.80686607e-03, 9.94785389e-03, 1.27595168e-02,
   8.88170288e-03, 7.44158290e-03, 9.98228524e-03, 1.63925465e-02,
   9.53423946e-03, 1.05554487e-02, 1.54851110e-02, 2.05146191e-02,
   1.30264012e-02, 7.20523041e-03, 1.83835207e-02, 1.00800192e-02,
   9.16292185e-03, 2.75197205e-02, 1.26532376e-02, 5.50722000e-03,
   8.06991937e-03, 4.67484356e-03, 9.11601494e-03, 9.94969137e-03,
   8.82691376e-03, 1.35812921e-02, 3.38891723e-02, 5.48777803e-02,
   5.47833810e-02, 3.91788967e-02, 2.34909778e-02, 1.59119884e-02,
   1.76145638e-02, 2.31054147e-02, 2.95776919e-02, 4.17103322e-02,
   2.07705960e-02, 1.08400124e-02, 6.43834017e-03, 4.46195474e-03,
   4.44256498e-03, 5.25020141e-03, 1.14951221e-02, 1.41466409e-02,
   1.73377342e-02, 1.41098637e-02, 7.04262356e-03, 3.99505900e-03,
   2.48446049e-03, 1.61739298e-03, 9.00339387e-04, 3.25213392e-04,
   2.93450309e-04, 3.10071719e-04, 2.85876075e-04, 1.78957305e-04,
   1.38316595e-04, 1.07859368e-04, 1.15453318e-04, 8.50033585e-05,
   7.25839567e-05, 3.80280533e-05, 3.57639077e-05, 2.47069514e-05,
   1.57980270e-05, 1.50489378e-05, 1.12732923e-05, 1.08223414e-05,
   1.21111000e-05, 1.57062296e-05, 1.11814270e-05, 9.70704523e-06,
   8.27147728e-06, 6.56079447e-06, 5.53983257e-06, 5.31472585e-06,
   2.77939077e-06, 1.77413323e-06, 1.03589758e-06, 6.50496670e-08])]

Since I already converted my whole dataset using this code above. Now I want to convert the above generated lists into a single list as I explained above. I hope this clarifies the problem. Could you help me out.

unpredictable
  • 320
  • 3
  • 13
  • 2
    What type of array are you using in your example? is it numpy? – A. Maman May 29 '20 at 05:29
  • Flattening a list is well documented. Please show the code you've written and explain where you're stuck. – Prune May 29 '20 at 05:29
  • @Prune Please look the problem now I have edited it. – unpredictable May 29 '20 at 05:41
  • where is the you tried toflatten the list – deadshot May 29 '20 at 05:42
  • Please reduce and enhance this into the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. The "example" you posted does not run, and does not appear to relate to the data you provided. – Prune May 29 '20 at 05:43
  • 1
    @unpredictable I believe you need `np.concatenate` or `np.hstack`. See https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html. – Alex May 29 '20 at 05:46
  • 1
    Use [`np.concatenate`](https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html) to join all array into one and then use [`tolist`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tolist.html) to convert that array into list. - `np.concatenate(lst).tolist()` – Dishin H Goyani May 29 '20 at 05:47

4 Answers4

2

Try using list.extend instead of append.

Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
tanmayjain69
  • 158
  • 1
  • 10
1

If you are using numpy you can use list.flatten. https://numpy.org/doc/1.18/reference/generated/numpy.ndarray.flatten.html

If you have a list of numpy arrays you can use np.hstack. https://numpy.org/doc/stable/reference/generated/numpy.hstack.html

Also see How to make a flat list out of list of lists? for general Python.

Alex
  • 947
  • 6
  • 16
0

Using List Comprehensions

items = [item for array in array.tolist() for item in array]

Using Numpy Module

np.concatenate(list).ravel().tolist()
Shafay
  • 187
  • 2
  • 7
0

Don't use python keyword (list) as a variable name. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive.

Use np.concatenate to join sequences of an array and tolist() to convert numpy array to list.

listt = [
         np.array([4.1,3.2,5.2]),
         np.array([4.1,8.5]),
         np.array([1.2,4.5,7.4])
        ]

np.concatenate(listt).tolist()
Hamza usman ghani
  • 2,264
  • 5
  • 19