Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'exist,': 0.09; 'function:': 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; '"new': 0.16; "'b',": 0.16; "['a',": 0.16; '_index': 0.16; 'dict': 0.16; 'increment': 0.16; 'names?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'variable': 0.18; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'directory.': 0.24; 'second': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'dialog': 0.31; 'invoke': 0.31; 'names.': 0.31; 'file': 0.32; 'this.': 0.32; 'created': 0.35; 'really': 0.36; 'list': 0.37; 'to:addr:python- list': 0.38; 'generating': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'letters': 0.60; 'save': 0.62; 'name': 0.63; 'saving': 0.69; 'hand': 0.80; 'subject:via': 0.84; 'lists:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: generating unique variable name via loops Date: Tue, 04 Nov 2014 14:09:42 +0100 Organization: None References: <8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bd9623.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415106598 news.xs4all.nl 2916 [2001:888:2000:d::a6]:33003 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80174 Fatih Güven wrote: > I want to generate a unique variable name for list using python. > > list1=... > list2=... > . > . > . > listx=... where x is a number. > > You can remember it from saving a file in a directory. If you have already > created a "new file", save dialog sugget that <<"new file " is already > exist, do you want to save it as "new file (1)"?>> so I want to apply it > for list names. > > Do you any idea about this. Do you really want to use list1, list2, list3, ... as variable names? You shouldn't. Instead use a dict or a list of lists: >>> list_of_lists = [] >>> list_of_lists.append([1, 2, 3]) >>> list_of_lists.append(["a", "b", "c"]) >>> list_of_lists.append(["foo", "bar", "baz"]) You can then access the second list with >>> letters = list_of_lists[1] >>> print(letters) ['a', 'b', 'c'] On the other hand if you just want to generate names an easy to understand approach is to increment a global variable every time you invoke the name- generating function: >>> _index = 1 >>> def next_name(): ... global _index ... name = "list{}".format(_index) ... _index += 1 ... return name ... >>> next_name() 'list1' >>> next_name() 'list2' >>> next_name() 'list3'