[Tutor] flatten a tuple

alan.gauld@bt.com alan.gauld@bt.com
Fri, 20 Apr 2001 15:14:41 +0100


| Convert the tuples to lists and change the print 
> | statements to result.append() and you should have 
> 
> What you really want is 'extend'

Nope, I want append. The recursive routine reduces 
the list to elements before printing thus you never 
attempt to print a list...

Here it is:
def printList(L):
    if not L: return
    if type(L[0]) == type([]): # extend for other sequences?
	printList(L[0])
    else: #no list so just print element
        print L[0] 
    # now process the rest of L 
    printList(L[1:])

I think it was Danny posted something similar already, 
that also did the reversal by using -1 instead of 0, 
a neat twist :-)

Alan G.

PS Is there a better way of testing the type? 
For example something that could check for any 
sequence? if L.hasMethod(__getitems__) or whatever?