Conversion of List of Tuples

Neil Cerutti neilc at norwich.edu
Tue Dec 4 08:54:39 EST 2012


On 2012-12-04, Hans Mulder <hansmu at xs4all.nl> wrote:
> It's considered bad style to use map it you don't want the list it
> produces.
>
>> There are more ways:
>> 
>>>>> from operator import add
>>>>> reduce(add, a)
>> (1, 2, 3, 4)
>
> There's a built-in that does "reduce(operator.add"; it's called "sum":
>
>>>> sum(a, ())
> (1, 2, 3, 4)

I thought that sort of thing would cause a warning. Maybe it's
only for lists.

Here's the recipe from the itertools documentation:

def flatten(listOfLists):
    "Flatten one level of nesting"
    return chain.from_iterable(listOfLists)

-- 
Neil Cerutti



More information about the Python-list mailing list