1

The answers at

How do I make a flat list out of a list of lists?

do not answer my question, I am talking about an inconsistent number of neste lists .

Suppose this is the list:

list_a = [
    [[1, 2]],
    [1, 1, 2],
    [[1, 2, 2], [3], [3,4]],
    [[[1,1], [1,2,3], [1,2,1]], [1,1]]
]

I would like to get all values in one list, like this:

list_b = [1, 2, 1, 1, 2, 1, 2, 2, 3, 3, 4, 1, 1, 1, 2, 3, 1, 2, 1, 1, 1]
Elicon
  • 206
  • 1
  • 11
  • You need to recursively flatten each level of the list. See for example @pylang answer on the duplicate. – Nick Oct 07 '22 at 07:34
  • @Nick I am talking about an inconsistent number of neste lists , The answers there do not help – Elicon Oct 07 '22 at 07:38
  • 1
    The answer I pointed to will absolutely flatten your structure: https://ideone.com/eBqQGx – Nick Oct 07 '22 at 07:42
  • 2
    This will solve: `def retrieve_valous(lst): new_list = [] for i in lst: if type(i) != list: new_list.append(i) else: new_list.extend(retrieve_valous(i)) return new_list` – Elicon Oct 07 '22 at 07:48
  • There are also other answers from EL_DON, pylang, mmj, Alon Gouldman, ... – Nick Oct 07 '22 at 07:48

0 Answers0