-2

I am trying to create a array from three kind of lists. Like l1, l2, l3. I am getting error says float is not iterable. How to unpack these lists into a 1-d list in python?

l1=[(260.3, 185.0), (268.01, 499.16)]
l2=[(268.01, 500.87), (678.9, 506.0)]
l3=((149.86, 354.48), (182.39, 344.2))
def unpack_lines(l1, l2, l3):
    out = []
    out.extend(l1[0][0])
    out.extend(l1[0][1])
    out.extend(l1[1][0])
    out.extend(l1[1][1])
    out.extend(l2[0][0])
    out.extend(l2[0][1])
    out.extend(l2[1][0])
    out.extend(l2[1][1])
    out.extend(l3[0][0])
    out.extend(l3[0][1])
    out.extend(l3[1][0])
    out.extend(l3[1][1])
    return out

unpack_lines(l1, l2, l3)

Error


TypeError                                 Traceback (most recent call last)
<ipython-input-27-6f84bf5b956a> in <module>
----> 1 unpack_lines(l1, l2, l3)

<ipython-input-26-159b13d00464> in unpack_lines(l1, l2, l3)
      1 def unpack_lines(l1, l2, l3):
      2     out = []
----> 3     out.extend(l1[0][0])
      4     out.extend(l1[0][1])
      5     out.extend(l1[1][0])

TypeError: 'float' object is not iterable

Expected output

[260.3,
185.0,
 268.01,
 499.16,
 268.01,
 500.87,
 678.9,
 506.0,
 149.86,
 354.48,
 182.39,
 344.2]
ajayramesh
  • 3,576
  • 8
  • 50
  • 75
  • `return list(itertools.chain.from_iterable(itertools.chain(l1, l2, l3)))` – han solo Aug 22 '20 at 18:09
  • 1
    `extend` takes an iterable. You probably meant `append`, or `extend(list[i])` – wjandrea Aug 22 '20 at 18:10
  • I wanted 1-D array for numpy, so i did extend - something like this I am trying - `[a, b, c, d, e, f, ..... ]` – ajayramesh Aug 22 '20 at 18:12
  • Does this answer your question? [How to make a flat list out of list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – wjandrea Aug 22 '20 at 18:17
  • 1
    @wjandrea - not really, they have just lists but in my question i have lists and tuple. It is similiar question, thank you – ajayramesh Aug 22 '20 at 18:29
  • @ajayramesh It's the same process, but yeah yours requires another nesting. This is also similar, but more complex: [Flatten an irregular list of lists](https://stackoverflow.com/q/2158395/4518341) – wjandrea Aug 22 '20 at 18:40

3 Answers3

2

You can just use itertools.chain.from_iterable over itertools.chain of those like,

>>> list(itertools.chain.from_iterable(itertools.chain(l1, l2, l3)))

Note: If all you need to do is iterate over the values, i'd drop the list creation, otherwise it is fine.

Although, i see you are working with numpy. Then you can just flatten the array like,

>>> import numpy as np
>>> 
>>> l1=[(260.3, 185.0), (268.01, 499.16)]
>>> l2=[(268.01, 500.87), (678.9, 506.0)]
>>> l3=((149.86, 354.48), (182.39, 344.2))
>>> 
>>> data = np.array([l1, l2, l3])
>>> data
array([[[260.3 , 185.  ],
        [268.01, 499.16]],

       [[268.01, 500.87],
        [678.9 , 506.  ]],

       [[149.86, 354.48],
        [182.39, 344.2 ]]])

>>> data.flatten()
array([260.3 , 185.  , 268.01, 499.16, 268.01, 500.87, 678.9 , 506.  ,
       149.86, 354.48, 182.39, 344.2 ])
>>> list(_)
[260.3, 185.0, 268.01, 499.16, 268.01, 500.87, 678.9, 506.0, 149.86, 354.48, 182.39, 344.2]
han solo
  • 6,390
  • 1
  • 15
  • 19
0

If your goal ist to simply flatten the lists of tupels, you can use this:

[item for sublist in zip(l1,l2,l3) for tupel in sublist for item in tupel]

Out[4]: 
[260.3,
 185.0,
 268.01,
 500.87,
 149.86,
 354.48,
 268.01,
 499.16,
 678.9,
 506.0,
 182.39,
 344.2]
Andreas
  • 8,694
  • 3
  • 14
  • 38
0

Use this recursive function.

def unpack(item):
    result = []
    if item is not None:
        if type(item) in [list, tuple]:
            for element in item:
                result.extend(unpack(element))
        elif type(item) is float:
            for elemtn in item:
                result.append(item)
    return result 
l1=[(260.3, 185.0), (268.01, 499.16)]
l2=[(268.01, 500.87), (678.9, 506.0)]
l3=((149.86, 354.48), (182.39, 344.2))
result = unpack(l1)+unpack(l2)+unpack(l3)
abdulsaboor
  • 678
  • 5
  • 10