[Tutor] flatten a tuple

Sean 'Shaleh' Perry shaleh@valinux.com
Wed, 18 Apr 2001 11:25:13 -0700 (PDT)


> 
> In general it's easier to convert tuples to lists for this sort of thing,
> then convert them back at the end. That's because it's easier to build
> things if they're mutable.
> 
> def flatten_reverse_tuple(tuples_tuple):
>    result = []
>    tuples_list = list(tuples_tuple)
>    tuples_list.reverse()
>    for tup in tuples_list:
>       result.extend(tup)
>    return tuple(result)
> 
> It doesn't completely flatten it, so that you won't get a big flat tuple if
> you have a tuple of tuples of tuples (I love saying that) but you didn't
> define what should happen in that case anyway.
> 

hmm, so much list conversion )-:  guess I will store them as lists.

BTW, list.extend() requires a list too, so it should have been
result.extend(list(tup)).