Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: list slice and generators Date: Wed, 25 Nov 2015 19:26:57 +0100 Organization: None Lines: 23 Message-ID: References: <5654D8CE.2020105@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de uH84uQseAhcyk0T5SHs0cAlaGmNHEIiIuHbTVJhOu+fg== 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; 'exercise': 0.03; 'elif': 0.04; 'metrics': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typeerror:': 0.09; 'wed,': 0.15; 'calculates': 0.16; 'len(b)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; '>>>': 0.20; '2015': 0.20; '"",': 0.22; 'am,': 0.23; '(most': 0.24; 'header :User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints- To:1': 0.26; 'function': 0.28; "skip:' 10": 0.28; 'complain': 0.29; 'that.': 0.30; 'traceback': 0.33; 'file': 0.34; 'that,': 0.34; 'list': 0.34; 'nov': 0.35; 'list,': 0.36; 'skip:i 20': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'list.': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'actually,': 0.84; 'otten': 0.84; 'average': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd95ef.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99474 Ian Kelly wrote: > On Wed, Nov 25, 2015 at 10:44 AM, Ian Kelly wrote: >> On Wed, Nov 25, 2015 at 3:07 AM, Peter Otten <__peter__@web.de> wrote: >>>> elif name in METRICS_AVG: >>> # writing a function that calculates the average without >>> # materialising the list left as an exercise ;) >> >> metrics = itertools.tee(metrics) >> return int(sum(metrics[0]) / len(metrics[1])) > > Actually, don't do that. It does effectively materialize the list, > just not as a list. Not just that, it will also complain >>> a, b = itertools.tee([1,2,3]) >>> sum(a), len(b) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'itertools._tee' has no len()