0

I already know lots of methods using which you can flatten list of lists.

For example :

Method 1:

A = [[1, 2], [3, 4], [5, 6]]
print sum(A, [])

Method 2

from itertools import chain
print list((chain.from_iterable(A)))

Method 3

 print [ val for sublist in A for val in sublist ]

But if your list are arranged in this way:

[[[[[[3]]]]], 'i', 1, 2] -> [3, 'i', 1, 2]
[[[[2]]], 1] -> [2, 1]
[[[[3]]], [[[2]]], [[[2]]], [[['y']]]] -> [3, 2, 2, 'y']

Nesting of lists can be of any depth. Could you help me here?

My idea is to use the type function in Python with something like

A = [[[[[[[3]]]]]]]

var = type(A)

while type(A) == var and type(A[0]) == var:
    A = A[0]

print A
python
  • 4,403
  • 13
  • 56
  • 103
  • @John Kugelman My question is different. I just don't have numbers, my list can be string, float, int. That question only covers int – python Jan 14 '16 at 02:14
  • The accepted answer looks like it will work with `int` and `float`. If it doesn't, it should be easy to adapt. – John Kugelman Jan 14 '16 at 02:26

0 Answers0