Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed3.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'skip:[ 20': 0.04; 'nested': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'list)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:recursion': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'example': 0.22; 'header:User- Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'especially': 0.30; "i'm": 0.30; 'code': 0.31; 'context,': 0.31; 'lists': 0.32; "can't": 0.35; 'but': 0.35; 'doing': 0.36; 'charset:us-ascii': 0.36; 'hi,': 0.36; 'being': 0.38; 'handle': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'does': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'course.': 0.60; 'flat': 0.60; 'simple': 0.61; 'more': 0.64; '(that': 0.65; 'imagine': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Understanding how is a function evaluated using recursion Date: Thu, 26 Sep 2013 00:26:23 +0000 (UTC) References: <231e5958-97c9-489c-9cfa-0f4451f6520c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.35 User-Agent: XPN/1.2.6 (Street Spirit ; Linux) 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380155208 news.xs4all.nl 15906 [2001:888:2000:d::a6]:56726 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54769 On 25/9/2013 19:24, Arturo B wrote: > Hi, I'm doing Python exercises and I need to write a function to flat nested lists > as this one: > > [[1,2,3],4,5,[6,[7,8]]] > > To the result: > > [1,2,3,4,5,6,7,8] > > So I searched for example code and I found this one that uses recursion (that I don't understand): > > def flatten(l): > ret = [] > for i in l: I can't imagine why you'd use either i or l in this context, but especially use them both when they look so much alike. > if isinstance(i, list) or isinstance(i, tuple): > ret.extend(flatten(i)) #How is flatten(i) evaluated? > else: > ret.append(i) > return ret > > So I know what recursion is, but I don't know how is > > flatten(i) > > evaluated, what value does it returns? > flatten() returns a list, of course. The value of 'ret' in the inner function. What don't you understand about recursion? You write a function that's valid for the simple case (a simple list, with none of the elements being lists or tuples). Then you use that function inside itself to handle the more complex cases. -- DaveA