Newbie: conventional instead of recursive way
Igor Zivkovic
izivkov1 at jagor.srce.hr
Wed Dec 18 16:19:52 EST 2002
Hello,
I have this function that prints each element of a list of strings. List
may be complex and contain other lists. If an element is a list then the
function calls itself recursively, if not then it prints the element.
def printList(L):
if not L:
return
if type(L[0]) == type([]):
printList(L[0])
else:
print L[0],
printList(L[1:])
The problem is that I don't know how to write the same function using a
conventional loop construct. This is one of my poor attempts:
def printList(L):
tmp = []
while L:
while type(L[0]) == type([]):
tmp.insert(0, L[1:])
L = L[:1]
tmp.insert(0, L[0])
L = tmp[:]
if type(L[0]) == type(1):
print L[0],
del L[0]
Could you help me, please ?
--
Igor
More information about the Python-list
mailing list