Write a Python function first_chars that takes one parameter, which is a nested list of strings. It returns a string containing the first character of all of the non-empty strings (at whatever depth they occur in the nested list) concatenated together. Here are a couple of examples of how the function should behave when you're done:
>>> first_chars(['Boo', 'is', 'happy', 'today']) 'Biht'
>>>first_chars(['boo', 'was', ['sleeping', 'deeply'], 'that', 'night', ['as', ['usual']]]) 'bwsdtnau'
def first_chars(lst): result = "" for x in lst: if type(x)==list: result += first_chars(x) else: result += x[0] return result # Testing print(first_chars(['Boo', 'is', 'happy', 'today'])) print(first_chars(['boo', 'was', ['sleeping', 'deeply'], 'that', 'night', ['as', ['usual']]]))
Get Answers For Free
Most questions answered within 1 hours.