0

I am trying to write a function that takes a nested tuple of strings and returns another tuple that contains all items of the original one, but "unpacked".

For example:

my_hobbies = ('reading', ('jogging', 'boxing', 'yoga'), 'movies').

TO

my_hobbies_unpacked = ('reading', 'jogging', 'boxing', 'yoga', 'movies')

I wrote the function below to accomplish this, but I am still receiving feedback that my answer is incorrect, how can I improve?

def unpack(input_tuple):
    mm = list(input_tuple)
    for x in mm:
        if isinstance(x, tuple):
            for y in x:
                mm.insert(-1,y)
            mm.remove(x)
    unpacked = tuple(mm)
    return unpacked
  • For a start, it falls at the first hurdle! What is `zz`? – Rolf of Saxony Jun 24 '20 at 17:24
  • That was a typo, my mistake – Mcclurkinma Jun 24 '20 at 17:26
  • Clearly it returns a single tuple, so what restrictions have been placed on the way that you can code the solution? There must be information/parameters missing from your question. – Rolf of Saxony Jun 24 '20 at 17:40
  • I think the function should be `re-entrant`, so when you find a `tuple` re-enter the function with the found tuple. If the item is not a tuple you can append it to your variable. You'll have to use a external variable to build the returnable result. – Rolf of Saxony Jun 24 '20 at 18:25

0 Answers0