Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'syntax': 0.04; 'subject:Python': 0.06; 'explicit': 0.07; 'indexing': 0.07; 'filter,': 0.09; 'occasionally': 0.09; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '[*]': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'reasonably': 0.16; 'subject: \n ': 0.16; 'such,': 0.16; 'syntax,': 0.16; 'throw': 0.16; 'elements': 0.16; 'wrote:': 0.18; "python's": 0.19; 'resolved': 0.19; 'written': 0.21; '(the': 0.22; '>>>': 0.22; 'code,': 0.22; 'import': 0.22; '(in': 0.22; 'cc:addr:python.org': 0.22; 'bytes': 0.24; 'connected': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'array': 0.29; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; 'that.': 0.31; '25,': 0.31; 'produces': 0.31; 'values.': 0.31; 'languages': 0.32; 'skip:c 30': 0.32; 'there,': 0.34; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'server': 0.38; 'filter': 0.38; 'mapping': 0.38; 'that,': 0.38; 'even': 0.60; 'simply': 0.61; "you'll": 0.62; 'hear': 0.63; 'map': 0.64; 'more': 0.64; 'talking': 0.65; 'effectively': 0.66; 'mar': 0.68; 'special': 0.74; 'subject:this': 0.83; 'actually,': 0.84; 'pike': 0.84; 'time)': 0.91; 'to:none': 0.92; 'wait,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=a9Ey5f7ohXI6QaaEeuPFfPI2A60Zzd6/ieg/V1C7Nxw=; b=E7XAXEoEooVkQrSBOeVybQJ1W52txfoMk7OqsvkYK1WEKn6uDLiUHEBLOz1U/QqKc3 YCguCzvUD7Ak8rEF2v/ziQBsucnxPfvhn2nYS5y43avmMqOOOtFVAbbds7Y6J2lBQsmh eoUv5De/WlGZwsY3Y2wE24u37roOxBN6XOZrYuoViH7+HJWRRgDj0Uy1/F+1lgffam1T AvvwyKXbuhZIMxRTY+Bk82vceEJNjmzTvfE/o85YppSGXOApQuNXb59piSrXf1D9HCpv 1crgxrDo7UKR4QOod5b6e+EN01tx6Vt7smqFXVXIZqP2F414TLOSsideyNWfANJA+sUd GvwQ== MIME-Version: 1.0 X-Received: by 10.68.200.74 with SMTP id jq10mr4056181pbc.169.1395688948778; Mon, 24 Mar 2014 12:22:28 -0700 (PDT) In-Reply-To: References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <532d5bd9$0$29994$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 25 Mar 2014 06:22:28 +1100 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395688958 news.xs4all.nl 2880 [2001:888:2000:d::a6]:54460 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68896 On Tue, Mar 25, 2014 at 5:58 AM, Mark H Harris wrote: > Its there, but its not on the built-ins; ie., you have to import it. The > confusion: why reduce, why not filter, nor map? {rhetorical} In other languages with those three, and without list/array comprehensions, I've used filter occasionally and map reasonably often, but I don't remember the last time I used reduce. Actually, Pike has special syntax that can take the place of map sometimes, so I might use filter more often than map in Pike code, because these don't need explicit map calls: //Suppose that clients is an array of connected clients on some server clients->sockets->write("System message: blah blah blah\n"); Indexing an array (the -> is like Python's . as Pike's . is resolved at compile time) produces an array, effectively mapping the elements through "lambda x: x->sockets" and ditto for "->write". Calling an array calls all the non-empty elements in it, with the same argument(s), and produces an array of return values. (In this case, I don't care about the return values, which will simply be the number of bytes written to each socket. If there's a problem, it'll throw an exception.) Huh. Even with that, and the [*] automap syntax, and such, I still use map far more often than filter... and filter orders of magnitude more often than reduce. Aside: You'll often hear people talking about "map-reduce" with big data. Python supports that. Look! >>> map.__reduce__ Oh wait, that's nothing to do with reduce()... *ducks for cover* ChrisA