eval ?

Cameron Laird claird at lairds.com
Fri Feb 20 09:19:12 EST 2004


In article <mailman.104.1077277571.27104.python-list at python.org>,
Angelo Secchi  <secchi at sssup.it> wrote:
>
>I'm trying to use eval (is the right function? ) to generate empty lists
>with different names(es. list_1, list_2, list_3, ...) in a loop similar
>to:
>
>for index in range(1,4):
>	list=[]
>	for index in range(1,7):
>		if <condition>:
>			list.append(1)
>	foo='list_'+str(index)+'=list'
>	eval(foo)
>
>I am not a programmer as you probably see from the code and I do not
>even know if this is the right approach to do that in Python (I used
>this structure with Matlab that I want now to dismiss ...)
			.
			.
			.
Matlab is valuable.  Python is even more so.  I think your move to Python
will reward you; you'll find new vistas of productivity opening up.  It
might interest you to read the references I'm starting to collect in <URL:
http://phaseit.net/claird/comp.programming/open_source_science.html >.

Yes, it's possible to do what you attempt above (although there are
several errors in the particular coding you've chosen), but it's far more
idiomatic to parametrize the lists this way:
  my_lists = {}
  for counter in range(1,5):
      my_lists[counter] = []

I suspect you want to do something more than just initialize my_lists; if
we understood your ultimate goal, it might turn out there's an even better
approach, perhaps one that sets up [] as a default value.
-- 

Cameron Laird <claird at phaseit.net>
Business:  http://www.Phaseit.net



More information about the Python-list mailing list