Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'represents': 0.05; 'nested': 0.07; 'logic': 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; 'different,': 0.16; 'mylist': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'meant': 0.20; 'header:User-Agent:1': 0.23; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'probably': 0.32; 'figure': 0.32; 'supposed': 0.32; 'guess': 0.33; 'totally': 0.33; "i'd": 0.34; 'problem': 0.35; 'subject:with': 0.35; 'something': 0.35; 'one,': 0.35; 'but': 0.35; 'really': 0.36; 'charset:us- ascii': 0.36; 'so,': 0.37; 'turn': 0.37; 'list': 0.37; 'follows:': 0.38; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'name': 0.63; 'real': 0.63; 'greetings': 0.72; 'all!': 0.84; 'exercise.': 0.84; 'subject:Lists': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Dealing with Lists Date: Tue, 10 Sep 2013 21:51:03 +0000 (UTC) References: <33650fe9-802e-477d-a361-6d4bdeb28762@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.36 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: 1378849885 news.xs4all.nl 15974 [2001:888:2000:d::a6]:35252 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53928 On 10/9/2013 17:08, stas poritskiy wrote: > Greetings to all! > > i ran into a little logic problem and trying to figure it out. > > my case is as follows: > > i have a list of items each item represents a Group > > i need to create a set of nested groups, > > so, for example: > > myGroups = ["head", "neck", "arms", "legs"] > > i need to get them to be represented like this: > (if you can imaging a folder structure) > > head > |_> neck > |_> arms > |_>legs I don't know what's meant by that |_> symbol. If it's really supposed to be like subdirectories, there'd be a name associated with the sublist. In which case you'd probably want dicts, not lists. So I'd have to guess you want something like: ["head", ["neck", ["arms", ["legs"]]]] To turn your list into that one, I'd write something like (untested): def chop_up(mylist): if len(mylist) == 1: return mylist return [mylist[0], chop_up(mylist[1:])] I suspect your real problem is totally different, but this was an interesting exercise. -- DaveA