Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Behaviour of list comprehensions Date: Tue, 02 Feb 2016 15:08:19 +0100 Organization: None Lines: 59 Message-ID: References: <4a9ac629-98fa-4643-af28-8d30b52bbc50@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 921mpcU0HIGsp7/+qYTdWglhyxeoI3M33VYfLyRi/6/A== 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; '"""': 0.05; 'subject:skip:c 10': 0.07; 'none.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'def': 0.13; "'b',": 0.16; 'error).': 0.16; 'instead:': 0.16; 'iteration': 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; 'tuples)': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'work,': 0.21; 'code.': 0.23; 'thus': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'behaviour': 0.29; 'once.': 0.29; 'code:': 0.29; 'convention': 0.30; 'run': 0.33; 'list': 0.34; 'returning': 0.35; 'but': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'means': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'called': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'no.': 0.62; 'email addr:gmail.com': 0.62; 'here': 0.66; 'therefore': 0.67; 'wrong!': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd93e6.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:102406 arsh.py@gmail.com wrote: > I am having some understandable behaviour from one of my function name > week_graph_data() which call two functions those return a big tuple of > tuples, But in function week_graph_data() line no. 30 does not > work(returns no result in graph or error). > > I have check both functions are called in week_graph_data() individually > and those run perfectly. (returning tuple of tuples) > > here is my code: pastebin.com/ck1uNu0U > def week_list_func(): > """ A function returning list of last 6 days in 3-tuple""" > > date_list = [] > for i in xrange(7): > d=date.today() - timedelta(i) > t = d.year, d.month, d.day > date_list.append(t) > return reversed(date_list) reversed(date_list) returns an "iterator": >>> items = reversed(["a", "b", "c"]) >>> items This means that iteration will "consume" the items >>> list(items) ['c', 'b', 'a'] >>> list(items) [] and thus work only once. To allow for multiple iterations you can return a list instead: >>> items = ["a", "b", "c"] >>> items.reverse() >>> items ['c', 'b', 'a'] >>> list(items) ['c', 'b', 'a'] >>> list(items) ['c', 'b', 'a'] Note that list.reverse() is a mutating method and by convention returns None. Therefore return date_list.reverse() # wrong! will not work, you need two lines date_list.reverse() return date_list in your code.