Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!solaris.cc.vt.edu!news.vt.edu!newsfeed-00.mathworks.com!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: How to convert a list of strings into a list of variables Date: Thu, 18 Aug 2011 16:09:43 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 30 Message-ID: References: <2ab25f69-6017-42a6-a7ef-c71bc2ee8547@l2g2000vbn.googlegroups.com> NNTP-Posting-Host: panix3.panix.com X-Trace: reader1.panix.com 1313683783 18882 166.84.1.3 (18 Aug 2011 16:09:43 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Thu, 18 Aug 2011 16:09:43 +0000 (UTC) User-Agent: nn/6.7.3 Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11775 In <2ab25f69-6017-42a6-a7ef-c71bc2ee8547@l2g2000vbn.googlegroups.com> noydb writes: > 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] > Thanks for any help! If the strings and the object names are exactly the same, you could use eval(). (Of course this assumes the objects already exist.) red = "this is the red object" one = 1 maple = "this is the maple object" list_of_strings = ["red", "one", "maple"] list_of_variables = [] for x in list_of_strings: list_of_variables.append(eval(x)) for y in list_of_variables: print y -- John Gordon A is for Amy, who fell down the stairs gordon@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies"