Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!news.ecp.fr!feeder1.cambriumusenet.nl!feed.tweaknews.nl!193.201.147.84.MISMATCH!xlned.com!feeder1.xlned.com!zen.net.uk!hamilton.zen.co.uk!reader02.news.zen.co.uk.POSTED!not-for-mail From: Nobody Subject: Re: How to convert a list of strings into a list of variables Date: Thu, 18 Aug 2011 19:45:28 +0100 User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) Message-Id: Newsgroups: comp.lang.python References: <2ab25f69-6017-42a6-a7ef-c71bc2ee8547@l2g2000vbn.googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lines: 18 Organization: Zen Internet NNTP-Posting-Host: d5a84efb.news.zen.co.uk X-Trace: DXC=\hEkUF[OMl>@IAOHEjT`\1YjZGX^207P;`NQ<`Sf8^Ta?O[GACV=\_?:fb[477J_\X2 X-Complaints-To: abuse@zen.co.uk Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11798 On Thu, 18 Aug 2011 16:09:43 +0000, John Gordon wrote: >> How would you convert a list of strings into a list of variables using >> the same name of the strings? > >> So, ["red", "one", "maple"] into [red, one, maple] > > If the strings and the object names are exactly the same, you could use > eval(). Eval is overkill for variables; use globals() and/or locals(). But data which is supposed to be indexed by a variable key (i.e. a name which is determined at run-time) should normally be put into a dictionary. If access with fixed keys is far more common than variable keys, using an object (with getattr/setattr for variable keys) may be preferable.