[Python-ideas] Map to Many Function

Guido van Rossum guido at python.org
Sun Aug 16 01:51:12 CEST 2015


On Sun, Aug 16, 2015 at 1:17 AM, Emile van Sebille <emile at fenx.com> wrote:

> On 8/15/2015 12:18 PM, Mark Tse wrote:
>
>> Currently, when the function for map() returns a list, the resulting
>> object
>> is an iterable of lists:
>>
>> list(map(lambda x: [x, x], [1, 2, 3, 4]))
>>>>>
>>>> [[1, 1], [2, 2], [3, 3], [4, 4]]
>>
>> However, a function to convert each element to multiple elements, similar
>> to flatMap (Java) or SelectMany (C#) does not exist, for doing the
>> following:
>>
>
> In addition to the itertools solutions already posted, there's also a
> flatten function that'll do it:
>
> Python 2.7.6 (default, Mar 22 2014, 22:59:56)
> [GCC 4.8.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from compiler.ast import flatten
> >>> flatten ((map(lambda x: [x, x], [1, 2, 3, 4])))
> [1, 1, 2, 2, 3, 3, 4, 4]
> >>>


That function isn't meant for public consumption though.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150816/d5f0496b/attachment-0001.html>


More information about the Python-ideas mailing list