-4

What I have so far:

def print_nested_list(input):
    """Prints out every single string in input, one per line."""
    if type(input) is list
           for item in input

thank you for the response - I get that rather than using just a for loop, I should have used that in combination with calling the print_nested_list function.

So to fulfill test cases:

print_nested_list(['cs1110'])
print_nested_list(['this', ['is', 'a'], 'list', ['list', 'list' ]])
print_nested_list([[['cs1110', 'opython'], 'nested'], 'recursion', 'test'])

To clarify, the last test case should look like this:

cs1110
opython
nested
recursion
test
Dave Lee
  • 9
  • 1
  • 6
  • Your current function is not recursive, and doesn't check whether anything is a list. You could definitely put a bit more effort in. – jonrsharpe Mar 26 '14 at 21:03
  • 1
    possible duplicate of [Flatten (an irregular) list of lists in Python](http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python) – roippi Mar 26 '14 at 21:03
  • Read about recursion first .. I would say – Andy897 Mar 26 '14 at 21:03

1 Answers1

1

I think you want to do this:

def print_nested_list(input_list):
    if type(input_list) is list:
        for item in input_list:
            print_nested_list(item)
    else:
        print input_list

mylist = [[['cs1110', 'opython'], 'nested'], 'recursion', 'test']
print_nested_list(mylist)

the output in this case is:

cs1110
opython
nested
recursion
test
Ammar
  • 1,305
  • 2
  • 11
  • 16
  • So instead of the "for item in ____ print item" loop I had, all I needed to do was call on the function I'm defining again within that loop to make it recursive? – Dave Lee Mar 26 '14 at 21:38
  • well, you edited your code, so now it is missing two things: 1-the recursive call of the function inside itself, 2-the condition to stop the recursion. I suggest you read more about recursion to get the whole picture. e.g: http://en.wikipedia.org/wiki/Recursion_%28computer_science%29 – Ammar Mar 26 '14 at 21:45