0

How to convert a list like this :

[[1,[2,["s",2],3]],[1,2,3,4,5,6],532, "Potato", [23, [[[1,[4,[2,[]],[0],[0], [0]]], 234, 1222], "22More"]]]

to

[1, 2, 's', 2, 3, 1, 2, 3, 4, 5, 6, 532, 'Potato', 23, 1, 4, 2, 0, 0, 0, 234, 1222, '22More']

I have tried using a for loop and then unpacking them using that but it didn't work out. Now I thought I could make variables of the element in the master list and then try unpacking them but it is not happing the way I want

Please do help

aryan vir
  • 11
  • 3
  • Please share what you tried, and be as precise as possible about what issues you are having with it. – Scott Hunter Feb 08 '22 at 17:41
  • Does this answer your question? [How to make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists) – Max Feb 08 '22 at 17:43
  • See i know how to flatten lists but somehow I wasn't able to create a proper recursive function – aryan vir Feb 09 '22 at 14:02

1 Answers1

0

you can use a recursive function to do that:

>>> list1 = [[1, [2, ['s', 2], 3]], [1, 2, 3, 4, 5, 6], 532, 'Potato', [23, [[[1, [4, [2, []], [0], [0], [0]]], 234, 1222], '22More']]]
>>> def listelements(mylist):
...     rslt = []
...     for value in mylist:
...             if isinstance(value,list):
...                     rslt.extend(_listconc(value))
...             else:
...                     rslt.append(value)
...     return rslt
...
>>> listelements(list1)
[1, 2, 's', 2, 3, 1, 2, 3, 4, 5, 6, 532, 'Potato', 23, 1, 4, 2, 0, 0, 0, 234, 1222, '22More']
XxJames07-
  • 1,833
  • 1
  • 4
  • 17