Flatten a list X91996


Statement
 

pdf   zip

html

Write a program that prints every given list flattened. For example, [[[a]], b] flattened is [a, b], and [[a, b], [c, d]] flattened is [a, b, c, d].

Define and use a recursive function

flatten(t)

that flattens a list t.

Hint

Using the ast package, you can extract the list t encoded in a string s with t = ast.literal_eval(s).

Input

The input consists of several lists.

Output

Print every given list flattened.

Public test cases
  • Input

    []
    ['a']
    [['a']]
    [[['a']]]
    ['a', 'b']
    ['a', []]
    [[], 'a']
    [['a'], 'b']
    ['a', ['b']]
    [[['a']], 'b']
    ['a', [['b']]]
    [['a', 'b'], ['c', 'd']]
    

    Output

    []
    ['a']
    ['a']
    ['a']
    ['a', 'b']
    ['a']
    ['a']
    ['a', 'b']
    ['a', 'b']
    ['a', 'b']
    ['a', 'b']
    ['a', 'b', 'c', 'd']
    
  • Information
    Author
    Gabriel Valiente
    Language
    English
    Official solutions
    Python
    User solutions
    Python