-1

I want to flatten my list that looks as follows:

generated = [[108.88114502], [19.29647502], [4.08611068], [52.33578872], [134.54672018], [14.22388561], [22.04375228]]

To something like this:

flat_list = [108.88114502, 19.29647502, 4.08611068, 52.33578872, 134.54672018, 14.22388561, 22.04375228]

I tried the following but I get a TypeError saying floats are unsubscriptable:

flat_list =  [item for sublist in generated for item in sublist]

Can someone help me out please?

WibeMan
  • 91
  • 7
  • Does this answer your question? [python list comprehensions; compressing a list of lists?](https://stackoverflow.com/questions/1077015/python-list-comprehensions-compressing-a-list-of-lists) – Chris Jun 29 '20 at 18:20
  • 1
    No it is a list, sorry I forgot the commas. However if I try @TrentonMcKinney 's way I get the typeError – WibeMan Jun 29 '20 at 18:37
  • I edited my code now @JohnColeman – WibeMan Jun 29 '20 at 18:39
  • If `generated` is exactly as edited then `[item for sublist in generated for item in sublist]` works fine, as you should be able to verify. Please read about the importance of providing a [mcve]. You have not provided enough information for others to replicate the problem. – John Coleman Jun 29 '20 at 18:40

2 Answers2

3

After OP edits and comments

  • TypeError saying floats are unsubscriptable when using [item for sublist in generated for item in sublist] means the list elements are single values and single values in a list (e.g. [[14.22388561], [22.04375228], 34.6576])
  • Check if each element is a list.
    • If item type is a list, take the value at index 0, assuming each list contains only 1 value.
    • If item type is not a list, take item
generated = [item[0] if type(item) == list else item for item in generated]

Solution from before OP edited the question

  • I'm guessing generated is a np.array, so you can use .reshape
    • If a np.array is printed, the output looks like that shown by the op, a list with no ,
    • The array shown, has the shape (7, 1), and you want it reshaped to (1, 7)
g = generated.reshape(1, 7)

# display g
array([[108.88114502,  19.29647502,   4.08611068,  52.33578872,
        134.54672018,  14.22388561,  22.04375228]])
  • If generated is a list, converting it to a np.array and using reshape will work.
generated = np.array(generated)

generated = generated.reshape(1, 7)  # where 7 is the length of the list)

%%timeit comparison

Given g as a list of lists

import numpy as np

np.random.seed(365)
g = [[np.random.randint(100000)/100] for _ in range(1000000)]
Test
%%timeit
[item for sublist in g for item in sublist]
74.6 ms ± 415 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

%%timeit
np.array(g).reshape(1, 1000000)
243 ms ± 2.51 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Given g as a (1000000, 1) np.array

np.random.seed(365)
g = [[np.random.randint(100000)/100] for _ in range(1000000)]
g = np.array(g)
Test
%%timeit
[item for sublist in g for item in sublist]
867 ms ± 3.11 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%%timeit
g.reshape(1, 1000000)
289 ns ± 1.16 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0
flat = [l[0] for l in generated]
zmbq
  • 38,013
  • 14
  • 101
  • 171