make a total list of a returned map (python)
Question: I have the lambda function f:

    
    
    f = lambda x:["a"+x, x+"a"]
    

and I have the list lst:

    
    
    lst = ["hello", "world", "!"]
    

So I did map on the function and the list to get a bigger list but it didn't
work as I thought:

    
    
    print map(f, lst)
    >>[ ["ahello", "helloa"], ["aworld", "worlda"], ["a!", "!a"] ]
    

As you can see I got lists inside list, but I wanted all of these strings to
be in **one** list

How can I do that?


Answer: Use
[`itertools.chain.from_iterable`](http://docs.python.org/2/library/itertools.html#itertools.chain.from_iterable):

    
    
    >>> import itertools
    >>> f = lambda x: ["a"+x, x+"a"]
    >>> lst = ["hello", "world", "!"]
    >>> list(itertools.chain.from_iterable(map(f, lst)))
    ['ahello', 'helloa', 'aworld', 'worlda', 'a!', '!a']
    

Alternative (list comprehension):

    
    
    >>> [x for xs in map(f, lst) for x in xs]
    ['ahello', 'helloa', 'aworld', 'worlda', 'a!', '!a']
    

