1

I need to unpack a nested list of this type:

lst = [5,2,3,[4,5, (6,7, [9])]]

to this:

[5, 2, 3, 4, 5, 6, 7, 9]

What I did:

def unpack_seq(sequence: List[Any]) -> List[Any]:
    final_lst = []
    for el in sequence:
        if isinstance(el, list) or isinstance(el, tuple):
            res = final_lst.append(unpack_seq(el))
        else:
            res = final_lst.append(el)
    return res
result =  unpack_seq([1,2,3,[4,5, (6,7, [9])]])

print(result)

And I get --> NONE

What's wrong with my code?

Please, don't advise using Flatten, I am not supposed to use it and want to understand what I did wrong here. I also used a function from GeekforGeeks, but it doesn't work as needed.

Thank you!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Kris
  • 23
  • 5
  • 4
    `append` modifies the list in place and returns `None`. – Barmar Jan 30 '23 at 17:48
  • So you should be returning `final_lst`, not the result from append. – Shmack Jan 30 '23 at 17:50
  • @user2736738 if I return 'final_lst' the result is this [1, 2, 3, [4, 5, [6, 7, [9]]]], and without append just '9' – Kris Jan 30 '23 at 18:06
  • Does this answer your question? [How do I make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists) – Mike 'Pomax' Kamermans Jan 30 '23 at 18:07
  • @Mike'Pomax'Kamermans no, it doesn't. The list there is easier, and I can't use the functions that are proposed there. But thanks :) – Kris Jan 30 '23 at 19:06
  • 1
    What do you mean "I can't use the functions that are proposed there"? Is this homework? Because that _radically_ changes the kind of answer you're looking for (real code and homework code are very much not the same thing ;) and should be mentioned in your post (asking about homework is fine, as long as you make it clear that you need an artificial answer, rather than a real world solution, which future visitors may not benefit from) – Mike 'Pomax' Kamermans Jan 30 '23 at 19:09
  • @Mike'Pomax'Kamermans exactly, this is homework :) oki, I'll be more precise next time – Kris Jan 31 '23 at 01:28
  • You have an [edit] button: please update _this_ post too, not just the next one. – Mike 'Pomax' Kamermans Jan 31 '23 at 02:02

2 Answers2

1

You receive None in output because the append() method returns nothing. Following your logic, I propose this solution:

def unpack_seq(sequence: Iterable[Any]) -> List[Any]:
    final_lst = []
    for el in sequence:
        if isinstance(el, list) or isinstance(el, tuple):
            final_lst.extend(unpack_seq(el)) 
        else:
            final_lst.append(el)
    return final_lst

For the 'sequence' parameter I advise you to replace the 'List' type by 'Iterable' because it could cause problems if you pass a tuple to the function.

0

Could be someone's homework, so I m not going to post everything.

res = final_lst.append(unpack_seq(el))

appends to final_lst, then returns None that gets stored to res...

I think you wanted to return the final_lst instead...

The problem is that you cannot just final_lst.append(unpack_seq(el)) because the result of unpack_seq(el) is a list, so you should add all its elements, not just append it... Checkout extend...

An alternative would have been to suppose its something that can be iterated and catch the exception if its not... [Edit:] As mentioned will only work with numbers though...

ntg
  • 12,950
  • 7
  • 74
  • 95
  • This implementation flattens things like strings too! Try `unpack(['foo', 'bar'])` to see what I mean. – Jasmijn Jan 30 '23 at 18:21
  • right, because they can be iterated as if they were a list of letters... :) This will flatten anything that has a length :) But the question was about a list of numbers... – ntg Jan 30 '23 at 18:29