1

Trying to unnest this list: [1, [2, 3, [4, 5, [6]]], [7, 8], 9]

Into this list: [1, 2, 3, 4, 5, 6, 7, 8, 9]

So far this is my function:

L = [1, [2, 3, [4, 5, [6]]], [7, 8], 9]

def unnesting(L):

     my_list = []

     for element in (L):
    
         if type(element) is list:             
             my_list.extend(element)
        
         else:
             my_list.append(element)
   
return my_list

except it gives me this output: [1, 2, 3, [4, 5, [6]], 7, 8, 9]

Any solutions or advice on how to unnest this list? Thank you!

buran
  • 13,682
  • 10
  • 36
  • 61
codekage
  • 43
  • 4

4 Answers4

1

You need recursion, otherwise, the function will only work for a nested depth of 2. The important realization is that when your list contains a list again, then you are facing the same problem, i.e. can call the same function again.

The following will work:

L = [1, [2, 3, [4, 5, [6]]], [7, 8], 9]


def unnest(lst1, lst2=None):
    if lst2 is None:
        lst2 = []
    for x in lst1:
        if not isinstance(x, list):
            lst2.append(x)
        else:
            unnest(x, lst2)

    return lst2


flattened = unnest(L)

print(flattened)
Sebastian
  • 865
  • 5
  • 13
  • Thank you for the suggestion. I'm actually told by my professor that: **Notice that, you should be able to use loops to solve this problem and should not use recursive functions (which we will not cover in this course).** – codekage Apr 17 '21 at 06:44
  • I also think I should use the extend function, and this function should work for any List, with the call being: **unnesting([1, [2, 3, [4, 5, [6]]], [7, 8], 9])** which unfortunately this does not work with this function – codekage Apr 17 '21 at 06:45
  • you need to call unnest and not unnesting – Sebastian Apr 17 '21 at 06:53
  • Also it is not clear to me how one would solve this general problem without recursion – Sebastian Apr 17 '21 at 06:56
0

I think you're just looking for a short recursive solution here, try this:

L = [1, [2, 3, [4, 5, [6]]], [7, 8], 9]

def unnesting(my_list):
    if type(my_list) == int:
        return [my_list]
    return sum((unnesting(elem) for elem in my_list), [])
    
print(unnesting(L))
Out: [1, 2, 3, 4, 5, 6, 7, 8, 9]

If you have any questions about how this works, leave a comment below and I'll do my best to help you understand.

LPR
  • 400
  • 1
  • 8
0

You can try this. I have just called the function recursively.

my_list = []

def unnesting(L):

    for element in L:
        if type(element) is list:
            unnesting(element)
        else:
            my_list.append(element)
    return my_list
Shivam Roy
  • 1,961
  • 3
  • 10
  • 23
0

well this is a little different way of doing this. I don't recommend doing it this way. But python allows us to get the result multiple ways.

def unnest(lst):
    _ = str(lst)
    _ = _.replace('[', '').replace(']', '').replace(',', '').replace(' ', '')
    return [int(i) for i in _]
L = [1, [2, 3, [4, 5, [6]]], [7, 8], 9]

unnest(L)

Deepak
  • 470
  • 1
  • 3
  • 15
  • when you convert list to string.It allows you remove extra characters.once you remove that you can convert back to list – Deepak Apr 17 '21 at 06:44