Newbie: conventional instead of recursive way

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Wed Dec 18 16:48:26 EST 2002


Igor Zivkovic <izivkov1 at jagor.srce.hr> writes:
> 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:])

def printList(L):
  for x in L:
    if type(x) == type([]):
       printList(x)
    else:
       print x



More information about the Python-list mailing list