Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'value,': 0.04; '[1,': 0.09; 'operand': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '2],': 0.16; '3],': 0.16; '[])': 0.16; 'last)': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'initial': 0.24; 'header:X -Complaints-To:1': 0.27; 'list:': 0.30; 'subject:list': 0.30; '"",': 0.31; 'file': 0.32; 'lists': 0.32; '(most': 0.33; "can't": 0.35; 'but': 0.35; 'doing': 0.36; 'list': 0.37; 'to:addr:python- list': 0.38; 'issue': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'most': 0.60; 'provide': 0.64; 'applying': 0.72; 'here...': 0.84; 'type(s)': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: zip list, variables Date: Wed, 20 Nov 2013 11:38:36 +0100 Organization: None References: <31cfb6e8-aa7e-46c2-ae0b-18d0d66e7bed@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084ad45.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384943908 news.xs4all.nl 15955 [2001:888:2000:d::a6]:46694 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60070 flebber wrote: > If > > c = map(sum, zip([1, 2, 3], [4, 5, 6])) > > c > Out[7]: [5, 7, 9] > > why then can't I do this? > > a = ([1, 2], [3, 4]) > > b = ([5, 6], [7, 8]) > > c = map(sum, zip(a, b)) > --------------------------------------------------------------------------- > TypeError Traceback (most recent call > last) in () > ----> 1 c = map(sum, zip(a, b)) > > TypeError: unsupported operand type(s) for +: 'int' and 'list' > > How can I do this legally? You are obscuring the issue with your map-zippery. The initial value of sum() is 0, so if you want to "sum" lists you have to provide a start value, typically an empty list: >>> sum([[1],[2]]) Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'list' >>> sum([[1],[2]], []) [1, 2] Applying that to your example: >>> def list_sum(items): ... return sum(items, []) ... >>> map(list_sum, zip(a, b)) [[1, 2, 5, 6], [3, 4, 7, 8]] Alternatively, reduce() does not require an initial value: >>> map(functools.partial(reduce, operator.add), zip(a, b)) [[1, 2, 5, 6], [3, 4, 7, 8]] But doing it with a list comprehension is the most pythonic solution here...